The array structure returned by the Yii model

I start Yii and stumble upon a wall and hope that someone can help me get back on track. I think this can be a pretty straightforward question for an experienced Yii user. So here goes ...

In the controller, let's say that I run the following model call -

$variable = Post::model()->findAll(); 

Everything works fine, and I pass the variable to the view. Here, where I'm pretty stuck. The array that is returned in the above query is much more complicated than I expected, and I'm struggling to figure it out. Here's the sample -

 print_r($variable); 

gives -

 Array ( [0] => Post Object ( [_md:CActiveRecord:private] => CActiveRecordMetaData Object ( [tableSchema] => CMysqlTableSchema Object ( [schemaName] => [name] => tbl_post [rawName] => `tbl_post` [primaryKey] => id [sequenceName] => [foreignKeys] => Array ( ) [columns] => Array ( [id] => CMysqlColumnSchema Object ( [name] => id [rawName] => `id` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [autoIncrement] => 1 [_e:CComponent:private] => [_m:CComponent:private] => ) [post] => CMysqlColumnSchema Object ( [name] => post [rawName] => `post` [allowNull] => [dbType] => text [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [_e:CComponent:private] => [_m:CComponent:private] => ) [columns] => Array ( [id] => CMysqlColumnSchema Object ( [name] => id [rawName] => `id` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [autoIncrement] => 1 [_e:CComponent:private] => [_m:CComponent:private] => ) [post] => CMysqlColumnSchema Object ( [name] => post [rawName] => `post` [allowNull] => [dbType] => text [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [relations] => Array ( [responses] => CHasManyRelation Object ( [limit] => -1 [offset] => -1 [index] => [through] => [joinType] => LEFT OUTER JOIN [on] => [alias] => [with] => Array ( ) [together] => [scopes] => [name] => responses [className] => Response [foreignKey] => post_id [select] => * [condition] => [params] => Array ( ) [group] => [join] => [having] => [order] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [attributeDefaults] => Array ( ) [_model:CActiveRecordMetaData:private] => Post 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 ( [id] => 1 [post] => User Post ) [_related:CActiveRecord:private] => Array ( ) [_c:CActiveRecord:private] => [_pk:CActiveRecord:private] => 1 [_alias:CActiveRecord:private] => t [_errors:CModel:private] => Array ( ) [_validators:CModel:private] => [_scenario:CModel:private] => update [_e:CComponent:private] => [_m:CComponent:private] => ) ) 

[sorry if there is an easier way to show this array, i don't know about it]

Can someone explain to me why the model returns such a complex array? It doesn't seem to matter which tables or columns or relationships are used in your application, they all return this format to me.

Also, can someone explain the structure to me so that I can isolate the variables I want to recover?

Thank you very much in advance,

Nick

+6
source share
3 answers

Better print_r

To get the best print_r result in yii, you can use CVarDumper class' dump() or dumpAsString() . They also provide the $highlight option, which helps you understand the result, format the output correctly, and add padding to it. Example:

 CVarDumper::dump($variables,10,true); // 10 is the default depth, and passing true will enable highlighting 

Why and what structure?

As mentioned in other answers, findAll() returns an array of CActiveRecord objects, so $variables is an array of objects, and $variables[0] is the first Post object. Yii CActiveRecord has many properties that are objects, for example a CActiveRecordMetaData object, which in turn has a CDbTableSchema object (and you have a subclass of CMysqlTableSchema, which means you are using mysql). print_r simply prints these objects, which are nothing more than the properties of the main CActiveRecord object. In addition to these objects, the attributes property (which is an array) of CActiveRecord contains your actual attribute values, so somewhere in the output you will also see such an array:

 [CActiveRecord:_attributes] => array ( 'attributeName' => 'attributeValue' 'anotherAttributeName' => 'anotherAttributeValue' 'someAttributeName' => 'someAttributeValue' ... ) 

These are your attribute values.


How to get access?

To access model properties, we can use both access to object properties and access to associative arrays (possibly because the CActiveRecord CModel parent class implements the php ArrayAccess Interface ). Example:

 $variables[0]->attributeName; $variables[0]['attributeName']; 

And since yii uses and overrides the __get php magic method we can do:

 $variables[0]->attributeName; // instead of $variables[0]->attributes['attributeName']; 

And of course, you can iterate over an array of Post objects using foreach() , as already shown in the following answer:

 foreach($variables as $aPost){ echo $aPost->attributeName; echo $aPost['attributeName']; echo $aPost->attributes['attributeName']; } 

To access relationships, simply use the relationship name:

 $variables[0]->relationName->attributeOfRelatedTable; $variables[0]['relationName']->attributeOfRelatedTable; $variables[0]['relationName']['attributeOfRelatedTable']; 

If your relation is HAS_MANY, then, of course, related models will also be returned as an array:

 $variables[0]->relationName[0]->attributeOfRelatedTable; $variables[0]['relationName'][0]->attributeOfRelatedTable; $variables[0]['relationName'][0]['attributeOfRelatedTable']; $variables[0]->relationName[0]['attributeOfRelatedTable']; 

And again, you can iterate over an array of relationships, taking into account the HAS_MANY relationships.

Edit: example for has_many iteration:

 foreach($variables as $aPost) { // get each post one by one echo $aPost->someAttribute; // or $aPost['someAttribute'] foreach($aPost->relationName as $aComment) { // say we get each comment of each post // or could have done $aPost['relationName'] as $aComment echo $aComment->commentAttribute; // or $aComment['commentAttribute'] } } 
+21
source

findall returns an array of active records for your model see here

After that, you can access all the columns in each record returned in this way.

 $results = Post::model()->findAll(); foreach($results AS $model) { echo $model->somecolumnname; echo $model->someothercolumnname; } 

This way, you don’t have to worry too much about all the details under the hood, as you can just use the abstraction directly.

+2
source

The simple answer to this is to use

 print_r($variable->attributes); 

where $variable is the object of the model class.

+1
source

Source: https://habr.com/ru/post/926626/


All Articles