无法从CGridView访问多个表

时间:2013-04-18 11:08:13

标签: database yii cgridview

当我尝试在yii中的单个CGridView中显示来自多个数据库表的数据时,我遇到了问题。

我搜索并提到了足够多的文章。但我无法解决我的问题。

我有2个表名为user_master和customer_master。对于每个客户,user_master中也会有一个主记录。表格如下 -

  

user_master - user_id,name,email_id,status    customer_master - customer_id,user_id,account_type,creation_date,status

在UserMaster.php中,代码是......

<?php
public function relations()
{
return array('customer_master'=>array(self::HAS_ONE, 'CustomerMaster', 'user_id'),);
}
public function searchUsers()
{
public $user_type;
$criteria=new CDbCriteria;
$criteria->alias = 't';
$criteria->compare('t.user_id',$this->user_id);
$criteria->compare('t.name',$this->name,true);
$criteria->compare('t.email_id',$this->email_id,true);
$criteria->compare('t.status',$this->status);

$criteria->together = true; 
$criteria->with = array('customer_master');
$criteria->compare('customer_master.user_id',$this->user_id,true);
$criteria->compare('customer_master.user_type',$this->user_type,true);

return new CActiveDataProvider($this, array('criteria'=>$criteria, 
            'sort'=>array('defaultOrder'=>'user_id ASC',),));
}
?>

在CustomerMaster.php ......

<?php
public function relations()
{
return array('user_id' => array(self::BELONGS_TO, 'UserMaster', 'user_id'),);
} ?>

UserMasterController.php的代码如下..

<?php
public function actionAdmin()
{
    $model=new UserMaster('search');
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['UserMaster']))
        $model->attributes=$_GET['UserMaster'];

        $this->render('admin',array(
            'model'=>$model, ));
}
?>

在userMaster / admin.php

<?php

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-master-grid',
    'dataProvider'=>$model->searchUsers(),
    'filter'=>$model,
    'columns'=>array(
        'user_id',
        'name',
        'status',
                'email_id',
        array(
            'name' => 'customer_master.user_type',
                        //'header' => 'customer_master.user_type',
            **'value' => $data->customer_master->user_type,**
                     ),

        array(
            'class'=>'CButtonColumn',
        ),
    ),
));
?>

通过调用userMaster / admin页面,我得到未定义变量:数据

请有人帮助我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试:

'value' => '$data->customer_master->user_type',  // as a string!

Value 是一个字符串,要作为PHP表达式进行评估......