1

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

$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 查询中获取表数据

4

2 回答 2

3

返回的数据是一个 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();
于 2013-01-09T13:07:06.097 回答
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)
于 2013-07-26T15:46:25.970 回答