我正在使用以下查询来建模
$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查询中获取表数据
答案 0 :(得分:3)
返回的数据是CActiveRecord对象。在返回的数据中具有这种复杂性是正常的。如果您只想获取数据库值,则应使用Yii中的DAO功能或查询构建器功能!
使用查询生成器的示例:
$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();
答案 1 :(得分:1)
我不确定你想要的是什么。但你可以看看我的样本来获得它。
$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)