如何在yii的CGridview中显示相关表中的数据 [英] How to display data from related tables in CGridview in yii

查看:20
本文介绍了如何在yii的CGridview中显示相关表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 CGridView 显示结果.我有两个表用户产品.ExiProducts 是维护 then 之间多对多关系的表,并让关系名称为 'myrelation'

I am trying to display the results using CGridView. i have two tables Users and products. ExiProducts is the table which maintains the many to many relation between then and let the relation name is 'myrelation'

public function actionSearch() {   
   if(isset($_GET['searchButton'] && $_GET['searchType']==='products') {
       $searchString=  trim(strip_tags($_GET['searchValue']));
       $model=new Products;
       $criteria->compare('productName', $searchString, TRUE, 'AND', TRUE);
       $criteria->compare('productType',$searchString,true,'OR',TRUE);
       $criteria->compare('productBrand',$searchString,true,'OR',TRUE);
       $criteria->compare('description',$searchString,true,'OR',true);

       $dataProviderObj=new CActiveDataProvider($model, array(
           'criteria'=>$criteria,
       ));  


   }

   $this->render('search',array(
       'dataProviderObj'=>$dataProviderObj,
        'model'=>$model,

   ));

}

这是我的view.php

 $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'users-grid',

        'dataProvider'=>$dataProviderObj,
        'columns'=>array(

            'productName',
            'productType',
            'productBrand',
            'description',
                    'I WANT THE NAME OF EVERY USER THAT CREATED THIS PRODUCT 
 HERE WHICH IS IN THE USERS TABLE '

        ),
 ));

谁能告诉我如何获得在那里创建这些产品的用户的姓名.users 表中的列是

Can somebody please tell me how i can get the name of the users creating those products there. columns in users table are

UserId,
Username

ExiProducts

UserId,
ProductId

更新了我的代码

public function gridCreateUser($data,$row) {
     $myproducts=array();

     $user = $data->userId;
     $records= Users::model()->with('usersproducts')->findAll('userId=:userId',array(':userId'=>$user));
        foreach($records as $record)
        {
            foreach($record->usersproducts as $productis)
            {
                $myproducts[]=$productis->productName;
            }

        }
        return $myproducts;


}

推荐答案

网格视图

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'users-grid',

    'dataProvider'=>$dataProviderObj,
    'columns'=>array(

        'productName',
        'productType',
        'productBrand',
        'description',
        array(
            'name' => '<column_name>'
            'value' => array($this,'gridCreateduser')
        )
     ),
));

这是你的网格视图 value =>array($this,'gridCreatedUser') 这意味着网格视图将在其控制器中搜索函数 gridCreateUser()

This is you grid view value => array($this,'gridCreatedUser') this means that grid view will search a function in its controller for a function gridCreateUser()

现在在控制器中

public function gridCreateUser($data,$row){

     $user = $data-><colmn_name>;
     //do your stuff for finding the username or name with $user
     //for eg.
     $detail = User::model()->findByPk($user);
     // make sure what ever model you are calling is accessible from this controller other wise you have to import the model on top of the controller above class of the controller.
     return $detail->username;
}

不,这会将该库名的所需值发送到网格视图.

No this will send the desired value of that coulmn name to grid view.

或者您可以通过在您正在创建的网格视图的模型内部定义模型之间的关系来以简单的方式使用

Or you can use in a simple manner by defining relation between models inside model whose gridview you are creating

public function relations(){
    return array(
        'users' => array(self::HAS_MANY, 'Users', '<column_name>'),
    );
}

然后你可以直接在你的网格视图中访问它

Then you can directly access it in you grid view

$this->widget('zii.widgets.grid.CGridView', array(
   'id'=>'users-grid',

   'dataProvider'=>$dataProviderObj,
   'columns'=>array(
       'productName',
       'productType',
       'productBrand',
       'description',
       array(
          'name' => '<column_name>'
          'value' => $data->users->username
       )
    ),
));

这篇关于如何在yii的CGridview中显示相关表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆