Php Yii'返回的数组结构;s模型

Php Yii'返回的数组结构;s模型,php,web-applications,yii,Php,Web Applications,Yii,我是一个Yii初学者,我遇到了一些困难,希望有人能帮助我回到正轨。我认为对于经验丰富的Yii用户来说,这可能是一个相当直截了当的问题。所以这里是 在控制器中,假设我运行以下对模型的调用- $variable = Post::model()->findAll(); 一切正常,我将变量传递到视图中。这就是我陷入困境的地方。在上面的查询中返回的数组比我预期的要复杂得多,我正在努力理解它。这是一个样品- print_r($variable); 给予- Array ( [0] => Pos

我是一个Yii初学者,我遇到了一些困难,希望有人能帮助我回到正轨。我认为对于经验丰富的Yii用户来说,这可能是一个相当直截了当的问题。所以这里是

在控制器中,假设我运行以下对模型的调用-

$variable = Post::model()->findAll();
一切正常,我将变量传递到视图中。这就是我陷入困境的地方。在上面的查询中返回的数组比我预期的要复杂得多,我正在努力理解它。这是一个样品-

print_r($variable);
给予-

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] => ) )
[很抱歉,如果有更简单的方法显示此阵列,我不知道]

有人能解释一下为什么模型返回如此复杂的数组吗?在应用程序中使用什么表、列或关系似乎无关紧要,在我看来,它们都返回这种格式

还有,有人能给我解释一下结构,这样我就可以分离出我想要恢复的变量了吗

多谢各位


Nick

findall返回模型的活动记录数组

一旦你有了它,你就可以像这样访问每个返回的记录中的所有列

$results = Post::model()->findAll();
foreach($results AS $model) 
{
    echo $model->somecolumnname;
    echo $model->someothercolumnname;
}
因此,您不必太在意引擎盖下的所有细节,因为您可以直接使用抽象。

更好的打印\r 要在yii中获得更好的
print\r
输出,可以使用类'
dump()
dumpAsString()
方法。它们还提供一个参数
$highlight
,通过正确格式化输出并向其添加缩进,可以帮助您理解输出。例如:

CVarDumper::dump($variables,10,true);
// 10 is the default depth, and passing true will enable highlighting
$variables[0]->attributeName;
$variables[0]['attributeName'];

为什么和什么结构? 如其他答案中所述,返回一个CActiveRecord对象数组,因此
$variables
是一个对象数组,
$variables[0]
是第一个Post对象。Yii的CActiveRecord有许多属性,这些属性都是对象,例如CActiveRecordMetaData对象,它又有一个CDbTableSchema对象(它的子类是CMysqlTableSchema,这意味着您正在使用mysql)。
print\r
只是简单地打印出这些对象,这些对象只是主CActiveRecord对象的属性。除了这些对象之外,CActiveRecord的
attributes
属性(是一个数组)还保存了您的实际属性值,因此在输出中的某个地方您还会看到如下数组:

[CActiveRecord:_attributes] => array
(
    'attributeName' => 'attributeValue'
    'anotherAttributeName' => 'anotherAttributeValue'
    'someAttributeName' => 'someAttributeValue'
    ...
)
这些是您的属性值


如何访问? 要访问模型的属性,我们可以同时使用对象属性访问和关联数组访问(可能是因为CActiveRecord的父类CModel实现了php)。例如:

CVarDumper::dump($variables,10,true);
// 10 is the default depth, and passing true will enable highlighting
$variables[0]->attributeName;
$variables[0]['attributeName'];
由于yii使用并覆盖了_get php magic方法,我们可以:

$variables[0]->attributeName;
// instead of 
$variables[0]->attributes['attributeName'];
当然,您可以使用
foreach()
迭代Post对象数组,如这里的另一个答案所示:

foreach($variables as $aPost){
    echo $aPost->attributeName;
    echo $aPost['attributeName'];
    echo $aPost->attributes['attributeName'];
}
要访问关系,只需使用关系名称:

$variables[0]->relationName->attributeOfRelatedTable;
$variables[0]['relationName']->attributeOfRelatedTable;
$variables[0]['relationName']['attributeOfRelatedTable'];
如果您的关系是HAS_MANY,那么当然相关模型也将作为数组返回:

$variables[0]->relationName[0]->attributeOfRelatedTable;
$variables[0]['relationName'][0]->attributeOfRelatedTable;
$variables[0]['relationName'][0]['attributeOfRelatedTable'];
$variables[0]->relationName[0]['attributeOfRelatedTable'];
同样,如果有许多关系,可以迭代relations数组

编辑:具有多个迭代的示例:

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']
    }
}

一个简单的答案是

print_r($variable->attributes);

其中,
$variable
是模型类的对象。

我没有使用Yii框架,但可能它们提供了一个var_dump替换。快速的谷歌搜索给了我“CVarDumper”,看看它是否取代了“print\r/var\u dump”。嗨,Clentfort,谢谢你的输入。不幸的是,它似乎没有以一种更容易理解的方式显示阵列。请看这篇文章:谢谢,这已经回答了我的问题以及我将要问的所有后续问题。非常感谢。谢谢所有帮助我的人。总是很乐意帮助我。我尽量做到彻底:)事实证明,我在“有很多”的关系上确实遇到了麻烦。我有多个帖子,每个帖子可能有多个回复。我想使用两个嵌套的foreach循环来迭代这些循环。但是我不能正确理解语法。我通常喜欢自己解决问题,但我在Yii这个元素上失败得很惨。我不认为你会在你的答案中添加解决方案?我想这可能会帮助我以外的其他初学者。我会欠你的债:)好的,添加了另一个代码示例,用于循环“有很多关系”。