6

我是一名 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] => ) )

[对不起,如果有更简单的方法来显示这个数组,我不知道]

谁能向我解释为什么模型返回如此复杂的数组?在您的应用程序中使用什么表、列或关系似乎并不重要,在我看来,它们都返回这种格式。

另外,有人可以向我解释结构,以便我可以隔离我想要恢复的变量吗?

提前谢谢了,

缺口

4

3 回答 3

21

更好的 print_r

要在 yii 中获得更好的print_r输出,您可以使用CVarDumperdump()dumpAsString()方法。它们还提供了一个参数$highlight,通过正确格式化输出并为其添加缩进,帮助您理解输出。例子:

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

为什么以及什么结构?

正如其他答案中已经提到的那样,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'
    ...
)

这些是您的属性值。


如何访问?

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

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

由于 yii 使用并覆盖了 __get php 魔术方法,我们可以这样做:

$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'];

再次,您可以在 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']
    }
}
于 2012-10-01T09:35:59.510 回答
2

findall 为您的模型返回一组活动记录,请参见此处

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

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

因此,您不必过多关注引擎盖下的所有细节,因为您可以直接使用抽象。

于 2012-10-01T06:08:45.383 回答
1

一个简单的答案是使用,

print_r($variable->attributes);

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

于 2013-02-28T11:29:47.480 回答