【问题标题】:Yii find query result - without table structureYii 查找查询结果 - 没有表结构
【发布时间】:2013-01-09 12:29:54
【问题描述】:

我正在对模型使用以下查询

$criteria = new CDbCriteria; 
$criteria->condition='brand_id=3';
$model=Models::model()->find($criteria);

它给出了如下表结构和关系表结构的结果

Models Object ( [_md:CActiveRecord:private] => CActiveRecordMetaData Object ( [tableSchema] => CMysqlTableSchema Object ( [schemaName] => [name] => models [rawName] => `models` [primaryKey] => model_id [sequenceName] => [foreignKeys] => Array ( [brand_id] => Array ( [0]
=> brands [1] => brand_id ) ) [columns] => Array ( [model_id] => CMysqlColumnSchema Object ( [name] => model_id [rawName] => `model_id` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [autoIncrement] => 1 [_e:CComponent:private] => [_m:CComponent:private] => ) [model_name] => CMysqlColumnSchema Object ( [name] => model_name [rawName] => `model_name` [allowNull] => [dbType] => varchar(255) [type] => string [defaultValue] => [size] => 255 [precision] => 255 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private]
=> ) [brand_id] => CMysqlColumnSchema Object ( [name] => brand_id [rawName] => `brand_id` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => [isForeignKey] => 1 [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [_e:CComponent:private] => [_m:CComponent:private] => ) [columns] => Array ( [model_id] => CMysqlColumnSchema Object ( [name] => model_id [rawName] => `model_id` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [autoIncrement] => 1 [_e:CComponent:private] => [_m:CComponent:private] => ) [model_name]
=> CMysqlColumnSchema Object ( [name] => model_name [rawName] => `model_name` [allowNull] => [dbType] => varchar(255) [type] => string [defaultValue] => [size] => 255 [precision] => 255 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [brand_id] => CMysqlColumnSchema Object ( [name] => brand_id [rawName] => `brand_id` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => [isForeignKey] => 1 [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [relations] => Array ( [brand] => CBelongsToRelation Object ( [joinType] => LEFT OUTER JOIN [on] => [alias] => [with] => Array ( ) [together] => [scopes] => [name] => brand [className] => Brands [foreignKey] => brand_id [select] => * [condition] => [params] => Array ( ) [group] => [join] => [having] => [order] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [attributeDefaults] => Array ( ) [_model:CActiveRecordMetaData:private] => Models Object ( [_md:CActiveRecord:private] => CActiveRecordMetaData Object
*RECURSION* [_new:CActiveRecord:private] => [_attributes:CActiveRecord:private] => Array ( ) [_related:CActiveRecord:private] => Array ( ) [_c:CActiveRecord:private] => [_pk:CActiveRecord:private] => [_alias:CActiveRecord:private] => t [_errors:CModel:private] => Array ( ) [_validators:CModel:private] => [_scenario:CModel:private] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [_new:CActiveRecord:private] => [_attributes:CActiveRecord:private] => Array ( [model_id] => 3 [model_name] => NANO [brand_id] => 3 ) [_related:CActiveRecord:private] => Array ( ) [_c:CActiveRecord:private] => [_pk:CActiveRecord:private] => 3 [_alias:CActiveRecord:private] => t [_errors:CModel:private] => Array ( ) [_validators:CModel:private] => [_scenario:CModel:private] => update [_e:CComponent:private] => [_m:CComponent:private] => )

如何只在 yii 查询中获取表数据

【问题讨论】:

    标签: php yii


    【解决方案1】:

    返回的数据是一个 CActiveRecord 对象。返回的数据有这样的复杂性是正常的。如果你只想获取 DB 值,你应该使用 Yii 的 DAO 功能或 Query Builder 功能!

    使用查询生成器的示例:

    $model = Yii::app()->db->createCommand()
        ->select('*')
        ->from('your_table t')
        ->where('brand_id=:id', array(':id'=>3))
        ->queryRow();
    

    使用 DAO 的示例:

    $connection=Yii::app()->db;
    $command=$connection->createCommand('SELECT * FROM your_table WHERE id =:id');
    $command->bindParam(":id", 3, PDO::PARAM_INT);
    $row=$command->queryRow();
    

    【讨论】:

      【解决方案2】:

      我不确定你想要什么。但是您可以查看我的示例来获取它。

      $words = Word::model()->findAll();
      $data=array_map(create_function('$m','return $m->getAttributes();'),$words);
      var_dump($data);
      

      我使用 ActiveRecord 从我的表词中获取所有记录,当然,您可以应用您的条件而不是“findAll”并根据您的需要进行不同的设置。这是我的结果。

      array (size=3)
        0 => 
          array (size=9)
            'word_id' => string '1' (length=1)
            'name' => string 'a' (length=1)
            'sound_file' => null
            'length' => string '1' (length=1)
            'type' => null
            'meaning' => null
            'priority' => string '1' (length=1)
            'first_char' => string 'a' (length=1)
            'word_count' => string '1' (length=1)
        1 => 
          array (size=9)
            'word_id' => string '2' (length=1)
            'name' => string 'b' (length=1)
            'sound_file' => null
            'length' => string '1' (length=1)
            'type' => null
            'meaning' => null
            'priority' => string '1' (length=1)
            'first_char' => string 'b' (length=1)
            'word_count' => string '1' (length=1)
        2 => 
          array (size=9)
            'word_id' => string '3' (length=1)
            'name' => string 'c' (length=1)
            'sound_file' => null
            'length' => string '1' (length=1)
            'type' => null
            'meaning' => null
            'priority' => string '1' (length=1)
            'first_char' => string 'c' (length=1)
            'word_count' => string '1' (length=1)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-30
        • 1970-01-01
        • 2023-03-10
        • 2013-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多