goo*_*ose 6 php web-applications yii
我是一名Yii初学者,并且遇到了一堵墙,希望有人能够帮助我回到正轨.我认为对于经验丰富的Yii用户来说这可能是一个相当直接的问题.所以这里......
在控制器中,假设我对模型运行以下调用 -
$variable = Post::model()->findAll();
Run Code Online (Sandbox Code Playgroud)
一切正常,我将变量传递给视图.这是我陷入困境的地方.在上面的查询中返回的数组比我预期的要复杂得多,我正在努力理解它.这是一个样本 -
print_r($variable);
Run Code Online (Sandbox Code Playgroud)
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] => ) )
Run Code Online (Sandbox Code Playgroud)
[对不起,如果有一种更简单的方式来显示这个数组,我不知道它]
任何人都可以向我解释为什么模型会返回如此复杂的数组?在您的应用程序中使用哪些表或列或关系似乎并不重要,在我看来它们都返回此格式.
此外,有人可以向我解释结构,以便我可以隔离我想要恢复的变量吗?
提前谢谢了,
缺口
boo*_*dev 21
要print_r在yii中获得更好的输出,可以使用CVarDumper类dump()或dumpAsString()方法.它们还提供了一个参数$highlight,通过正确格式化输出并为其添加缩进,可帮助您了解输出.例:
CVarDumper::dump($variables,10,true);
// 10 is the default depth, and passing true will enable highlighting
Run Code Online (Sandbox Code Playgroud)
正如在其他答案中已经提到的那样,findAll()返回一个CActiveRecord对象数组,因此$variables是一个对象数组,并且$variables[0]是第一个Post对象.Yii的CActiveRecord有许多属性作为对象,例如CActiveRecordMetaData对象,后者又有一个CDbTableSchema对象(你有它的子类CMysqlTableSchema,这意味着你使用的是mysql).在print_r简单地打印出这些对象,这是什么,但主要的CActiveRecord对象的属性.除了这些对象之外,attributesCActiveRecord 的属性(它是一个数组)保存了您的实际属性值,因此在输出中的某个位置您还会看到如下数组:
[CActiveRecord:_attributes] => array
(
'attributeName' => 'attributeValue'
'anotherAttributeName' => 'anotherAttributeValue'
'someAttributeName' => 'someAttributeValue'
...
)
Run Code Online (Sandbox Code Playgroud)
这些是你的属性值.
要访问模型的属性,我们可以使用对象属性访问和关联数组访问(可能是因为CActiveRecord的父类CModel实现了php的ArrayAccess接口).例:
$variables[0]->attributeName;
$variables[0]['attributeName'];
Run Code Online (Sandbox Code Playgroud)
由于yii使用并覆盖了__get php magic方法,我们可以这样做:
$variables[0]->attributeName;
// instead of
$variables[0]->attributes['attributeName'];
Run Code Online (Sandbox Code Playgroud)
当然,您可以使用foreach()另一个答案中显示的方式迭代Post对象数组:
foreach($variables as $aPost){
echo $aPost->attributeName;
echo $aPost['attributeName'];
echo $aPost->attributes['attributeName'];
}
Run Code Online (Sandbox Code Playgroud)
要访问关系,只需使用关系名称:
$variables[0]->relationName->attributeOfRelatedTable;
$variables[0]['relationName']->attributeOfRelatedTable;
$variables[0]['relationName']['attributeOfRelatedTable'];
Run Code Online (Sandbox Code Playgroud)
如果您的关系是HAS_MANY,那么相关模型当然也将作为数组返回:
$variables[0]->relationName[0]->attributeOfRelatedTable;
$variables[0]['relationName'][0]->attributeOfRelatedTable;
$variables[0]['relationName'][0]['attributeOfRelatedTable'];
$variables[0]->relationName[0]['attributeOfRelatedTable'];
Run Code Online (Sandbox Code Playgroud)
再次,你可以遍历HAS_MANY关系的关系数组.
编辑:has_many迭代的示例:
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']
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12545 次 |
| 最近记录: |