由FindAll方法返回的ActiveRecord结果需要不同的格式

时间:2014-04-06 14:39:54

标签: yii

我执行了以下声明 $posts = Post::model()->with(array( 'category', 'member', 'profile' ))->findAll();

我在日志文件中打印了$ posts。我得到了以下结果。

Array
(
    [0] => Post Object
        (
            [_new:CActiveRecord:private] => 
            [_attributes:CActiveRecord:private] => Array
                (
                    [id] => 1
                    [post_text] => Sales needs to be increase by the end of this month. I don't wnat any reason behind this.
                    [member_id] => 2
                    [category_id] => 3
                    [published] => 0
                    [draft] => 1
                    [date_added] => 2014-04-06
                )

            [_related:CActiveRecord:private] => Array
                (
                    [category] => Category Object
                        (
                            [_new:CActiveRecord:private] => 
                            [_attributes:CActiveRecord:private] => Array
                                (
                                    [id] => 1
                                    [category_name] => Anything
                                    [type] => General
                                    [date_added] => 2014-04-05
                                )

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

                    [member] => Member Object
                        (
                            [_new:CActiveRecord:private] => 
                            [_attributes:CActiveRecord:private] => Array
                                (
                                    [id] => 1
                                    [screen_name] => prashantb
                                    [email] => prashantbharambe22@gmail.com
                                    [date_added] => 2014-04-05
                                    [first_name] => prashant
                                    [last_name] => bharambe
                                )

                            [_related:CActiveRecord:private] => Array
                                (
                                    [profile] => MemberProfile Object
                                        (
                                            [_new:CActiveRecord:private] => 
                                            [_attributes:CActiveRecord:private] => Array
                                                (
                                                    [id] => 1
                                                    [member_id] => 1
                                                    [city] => kalyan
                                                    [state] => maharashtra
                                                    [country] => india
                                                    [designation] => php developer
                                                    [date_added] => 2014-04-05
                                                )

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

                                )

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

                )

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

但我需要采用以下格式的结果。

Array
(
    [0] => Array 
    (
        [id] => 1,
        [post_text] => Sales needs to be increase by the end of this month. I don't wnat any reason behind this,
        [member_id] => 2,
        [category_id] => 3,
        [published] => 0,
        [draft] => 1,
        [date_added] => 2014-04-06,
        [category] => Array (
            [id] => 1,
            [category_name] => Anything,
            [type] => General,
            [date_added] => 2014-04-05,
        ),
        [member] => Array (
            [id] => 1,
            [screen_name] => prashantb,
            [email] => prashantbharambe94@gmail.com,
            [date_added] => 2014-04-05,
            [first_name] => prashant,
            [last_name] => bharambe,
            [profile] => Array(
                [id] => 1
                [member_id] => 1
                [city] => kalyan
                [state] => maharashtra
                [country] => india
                [designation] => php developer
                [date_added] => 2014-04-05
            )
        )
    )
)

简而言之,我需要一个数组中的所有属性值。 (模型属性及其相关模型属性)

我是yii的新手。请帮帮我。

1 个答案:

答案 0 :(得分:0)

Yii不适用于数组。每个DB记录由ActiveRecord对象表示。这样您就可以访问列值,如下所示:

echo $model->post_text; // Sales needs to be increase...
echo $model->date_added; // 2014-04-06
echo $model->etc...

如果你确实设置了正确的关系,你可以像这样访问这些数据:

echo $model->category->category_name; // Anything
echo $model->member->screen_name; // prashantb

您必须手动创建阵列。如果您仍需要数组中的数据,还可以访问attributes值:

print_r($model->attributes); // array( [id] => 1, [post_text] => 'Sales needs to be increase', ...)