代码之家  ›  专栏  ›  技术社区  ›  friedFingers

yii-cgridview关系多级

  •  0
  • friedFingers  · 技术社区  · 11 年前

    我有4张订单支付用户和个人资料表。付款有一个 belongs_to 与秩序的关系。订单有一个 属于_ 与用户的关系以及用户拥有许多配置文件。

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

    我尝试使用:

    $data->order->user->profiles->firstname;
    

    还尝试将参数firstname添加到Payment的模型类中,并尝试将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,
            ));
        }
    
    2 回复  |  直到 11 年前
        1
  •  1
  •   Balaji Viswanath    11 年前

    我建议使用“通过”关系,因为这会让生活更轻松。你所要做的就是,转到你的“支付”模型,并添加以下关系,

    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'
            ),
        );
    }
    

    在网格中,您可以使用访问first_name,

    $data->profiles[0]->firstname
    
        2
  •  0
  •   Olegacy    11 年前

    在模型中尝试此操作:

    public function getFirstname(){
        return $this->order->user->profiles->firstname;
    }
    

    并且在网格中:

    $data->firstname;