yii cgridview关系多级

yii cgridview关系多级,yii,cgridview,database-relations,Yii,Cgridview,Database Relations,我有4张订单付款表和用户配置文件。付款与订单有关系。订单与用户有属于关系,用户有多个配置文件 在cgridview中显示付款时,我需要显示存储在配置文件中的用户的名字和姓氏 我尝试使用: $data->order->user->profiles->firstname; 还尝试将参数firstname添加到付款模型类,并尝试创建setter方法,如下所示: public function getFirstname(){ if ($this->_firstna

我有4张订单付款表和用户配置文件。付款与订单有
关系。订单与用户有
属于
关系,用户有多个配置文件

在cgridview中显示付款时,我需要显示存储在配置文件中的用户的名字和姓氏

我尝试使用:

$data->order->user->profiles->firstname;
还尝试将参数firstname添加到付款模型类,并尝试创建setter方法,如下所示:

public function getFirstname(){
    if ($this->_firstname=== null && $this->order !== null) {
                $this->_firstname = $this->order->user->profiles->firstname;
            }
            return $this->_firstname ;
    }
    public function setFirstname($value){
        $this->_firstname = $value ;
    }
但我一直未能达到预期的效果

编辑:搜索方法具有以下代码:

public function search() {
        $criteria = new CDbCriteria;
        $criteria->with = array('order.user.profiles') ;
. . . .
        $criteria->compare('firstname', $this->_firstname, true);

. . . .     
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
在模型中尝试以下操作:

public function getFirstname(){
    return $this->order->user->profiles->firstname;
}
在网格中:

$data->firstname;
我建议使用“通过”关系,因为它使生活更轻松。您所要做的就是转到“付款”模型并添加以下关系

public function relations()
{
    return array(
        'order' => array(self::BELONGS_TO, 'Orders', 'order_id'),
        'user'=>array(
            self::BELONGS_TO,'User',array('user_id'=>'id'),'through'=>'order'
        ),
        'profiles'=>array(
            self::HAS_MANY,'Profile',array('id'=>'user_id'),'through'=>'user'
        ),
    );
}
在网格中,您可以使用

$data->profiles[0]->firstname