Yii find query result - without table structure

2019-08-09 01:00发布

问题:

I'm using the below query for to models

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

it gives result with Table structure and relational table structure like below

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] => )

How can i get table data only in yii queries

回答1:

The returned data is a CActiveRecord object. It's normal to have such complexity in the returned data. If you want to obtain just the DB values, you should use the DAO feature or the Query Builder feature from Yii!

  • DAO in Yii Guide
  • Query Builder in yii Guide

Example using Query Builder:

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

Example using 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:

I'm not sure what you want in your end. But you could look at my sample to get it.

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

I use ActiveRecord to get all records from my table word, Of cause, you can apply your criteria instead "findAll" and make it differently for your need .Here is my result.

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)


标签: php yii