CActiveRecord和“试图获取非对象属性”错误

时间:2012-08-30 15:36:10

标签: yii

在带有CGridView的Yii Framework中,我需要从CActiveRecord中读取一个值

CGridView中的列定义适用于列:

'columns'=>array(
        ....
        array(
            'name'=>'rel_CustomerCampain.event_comment',
            'header'=>'Commenti Camp.',
            'value'=>'print_r($data->rel_CustomerCampain,false)'
        ),
.....

我有这个对象$ data-> rel_CustomerCampain,我需要在“_attributes:CActiveRecord:private”下的event_comment中获取值: 的 * IneedOfThisValue **

当我尝试   '值'=> '$ DATA-> rel_CustomerCampain-> event_comment'

我有这个错误: “试图获取非对象的属性”

谁有想法? 提前谢谢....

[0] => customerCampain Object
        (
            [_md:CActiveRecord:private] => CActiveRecordMetaData Object
                (
                    [tableSchema] => CMysqlTableSchema Object
                        (
                            [schemaName] => 
                            [name] => tbl_customer_campain
                            [rawName] => `tbl_customer_campain`
                            [primaryKey] => id
                            [sequenceName] => 
                            [foreignKeys] => Array
                                (
                                    [id_campain] => Array
                                        (
                                            [0] => tbl_campain
                                            [1] => id
                                        )

                                    [id_customer] => Array
                                        (
                                            [0] => tbl_customer
                                            [1] => id
                                        )

                                )

                            [columns] => Array
                                (
                                    [id] => CMysqlColumnSchema Object
                                        (
                                            [name] => id
                                            [rawName] => `id`
                                            [allowNull] => 
                                            [dbType] => int(10) unsigned
                                            [type] => string
                                            [defaultValue] => 
                                            [size] => 10
                                            [precision] => 10
                                            [scale] => 
                                            [isPrimaryKey] => 1
                                            [isForeignKey] => 
                                            [autoIncrement] => 1
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [id_campain] => CMysqlColumnSchema Object
                                        (
                                            [name] => id_campain
                                            [rawName] => `id_campain`
                                            [allowNull] => 
                                            [dbType] => int(10) unsigned
                                            [type] => string
                                            [defaultValue] => 
                                            [size] => 10
                                            [precision] => 10
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 1
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [id_customer] => CMysqlColumnSchema Object
                                        (
                                            [name] => id_customer
                                            [rawName] => `id_customer`
                                            [allowNull] => 
                                            [dbType] => int(10) unsigned
                                            [type] => string
                                            [defaultValue] => 
                                            [size] => 10
                                            [precision] => 10
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 1
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [event_datetime] => CMysqlColumnSchema Object
                                        (
                                            [name] => event_datetime
                                            [rawName] => `event_datetime`
                                            [allowNull] => 1
                                            [dbType] => datetime
                                            [type] => string
                                            [defaultValue] => 
                                            [size] => 
                                            [precision] => 
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [event_type] => CMysqlColumnSchema Object
                                        (
                                            [name] => event_type
                                            [rawName] => `event_type`
                                            [allowNull] => 1
                                            [dbType] => varchar(200)
                                            [type] => string
                                            [defaultValue] => 
                                            [size] => 200
                                            [precision] => 200
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [event_status] => CMysqlColumnSchema Object
                                        (
                                            [name] => event_status
                                            [rawName] => `event_status`
                                            [allowNull] => 1
                                            [dbType] => varchar(200)
                                            [type] => string
                                            [defaultValue] => 
                                            [size] => 200
                                            [precision] => 200
                                            [scale] => 
                                            [isPrimaryKey] => 
                                            [isForeignKey] => 
                                            [autoIncrement] => 
                                            [_e:CComponent:private] => 
                                            [_m:CComponent:private] => 
                                        )

                                    [event_comment] => CMysqlColumnSchema Object
                                        (
                                            [name] => event_comment
                                            [rawName] => `event_comment`
                                            [allowNull] => 1
                                            [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(10) unsigned
                                    [type] => string
                                    [defaultValue] => 
                                    [size] => 10
                                    [precision] => 10
                                    [scale] => 
                                    [isPrimaryKey] => 1
                                    [isForeignKey] => 
                                    [autoIncrement] => 1
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [id_campain] => CMysqlColumnSchema Object
                                (
                                    [name] => id_campain
                                    [rawName] => `id_campain`
                                    [allowNull] => 
                                    [dbType] => int(10) unsigned
                                    [type] => string
                                    [defaultValue] => 
                                    [size] => 10
                                    [precision] => 10
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 1
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [id_customer] => CMysqlColumnSchema Object
                                (
                                    [name] => id_customer
                                    [rawName] => `id_customer`
                                    [allowNull] => 
                                    [dbType] => int(10) unsigned
                                    [type] => string
                                    [defaultValue] => 
                                    [size] => 10
                                    [precision] => 10
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 1
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [event_datetime] => CMysqlColumnSchema Object
                                (
                                    [name] => event_datetime
                                    [rawName] => `event_datetime`
                                    [allowNull] => 1
                                    [dbType] => datetime
                                    [type] => string
                                    [defaultValue] => 
                                    [size] => 
                                    [precision] => 
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [event_type] => CMysqlColumnSchema Object
                                (
                                    [name] => event_type
                                    [rawName] => `event_type`
                                    [allowNull] => 1
                                    [dbType] => varchar(200)
                                    [type] => string
                                    [defaultValue] => 
                                    [size] => 200
                                    [precision] => 200
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [event_status] => CMysqlColumnSchema Object
                                (
                                    [name] => event_status
                                    [rawName] => `event_status`
                                    [allowNull] => 1
                                    [dbType] => varchar(200)
                                    [type] => string
                                    [defaultValue] => 
                                    [size] => 200
                                    [precision] => 200
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [event_comment] => CMysqlColumnSchema Object
                                (
                                    [name] => event_comment
                                    [rawName] => `event_comment`
                                    [allowNull] => 1
                                    [dbType] => text
                                    [type] => string
                                    [defaultValue] => 
                                    [size] => 
                                    [precision] => 
                                    [scale] => 
                                    [isPrimaryKey] => 
                                    [isForeignKey] => 
                                    [autoIncrement] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                        )

                    [relations] => Array
                        (
                            [relCustomer] => CBelongsToRelation Object
                                (
                                    [joinType] => LEFT OUTER JOIN
                                    [on] => 
                                    [alias] => 
                                    [with] => Array
                                        (
                                        )

                                    [together] => 
                                    [scopes] => 
                                    [name] => relCustomer
                                    [className] => customer
                                    [foreignKey] => id_customer
                                    [select] => *
                                    [condition] => 
                                    [params] => Array
                                        (
                                        )

                                    [group] => 
                                    [join] => 
                                    [having] => 
                                    [order] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                            [idCampain] => CBelongsToRelation Object
                                (
                                    [joinType] => LEFT OUTER JOIN
                                    [on] => 
                                    [alias] => 
                                    [with] => Array
                                        (
                                        )

                                    [together] => 
                                    [scopes] => 
                                    [name] => idCampain
                                    [className] => TblCampain
                                    [foreignKey] => id_campain
                                    [select] => *
                                    [condition] => 
                                    [params] => Array
                                        (
                                        )

                                    [group] => 
                                    [join] => 
                                    [having] => 
                                    [order] => 
                                    [_e:CComponent:private] => 
                                    [_m:CComponent:private] => 
                                )

                        )

                    [attributeDefaults] => Array
                        (
                        )

                    [_model:CActiveRecordMetaData:private] => customerCampain 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] => 2155
                    [id_campain] => 4
                    [id_customer] => 109
                    [event_datetime] => 
                    [event_type] => 
                    [event_status] => 
                    [event_comment] => ******IneedOfThisValue*****
                )

            [_related:CActiveRecord:private] => Array
                (
                )

            [_c:CActiveRecord:private] => 
            [_pk:CActiveRecord:private] => 2155
            [_alias:CActiveRecord:private] => t
            [_errors:CModel:private] => Array
                (
                )

            [_validators:CModel:private] => 
            [_scenario:CModel:private] => update
            [_e:CComponent:private] => 
            [_m:CComponent:private] => 
        )

)

1 个答案:

答案 0 :(得分:1)

由于我怀疑 1 ,您当前的模型(即您正在显示gridview的模型)与 CustomerCampain 模型之间存在HAS_MANY关系。这意味着$data->rel_CustomerCampain CustomerCampain 对象的数组,也可以从print_r输出中看到:

[0] => customerCampain Object
     (
      ...
     )

所以而不是:

'value'=>'$data->rel_CustomerCampain->event_comment'

你必须这样做:

'value'=>'$data->rel_CustomerCampain[arrayIndex]->event_comment'
// here arrayIndex will have to be the index of the campain you are interested in
'value'=>'$data->rel_CustomerCampain[0]->event_comment' // 0 index for the first campain

(1):先看comment来提问