php - yii中如何显示CGridview中相关表的数据

标签 php yii cgridview

我正在尝试使用 CGridView 显示结果。我有两个表 UsersproductsExiProducts是维护then之间多对多关系的表,关系名称为'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 表中的列是

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;


}

最佳答案

GridView View

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

这是你的 GridView value => array($this,'gridCreatedUser')这意味着 GridView 将在其 Controller 中搜索函数 gridCreateUser()

现在在 Controller 中

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;
}

不,这会将该列名称的所需值发送到 GridView 。

或者您可以通过在您正在创建其 GridView 的模型中定义模型之间的关系来以简单的方式使用

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

然后你可以直接在你的 GridView 中访问它

$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
       )
    ),
));

关于php - yii中如何显示CGridview中相关表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20285509/

相关文章:

Yii 标准 : condition for relation

mysql - 如何连接三个表并在 GridView 中获取值

mysql - Yii CGridView 基于关系 HAS_MANY 数据建表

php - 扩展或包含——Twig 中有什么更好的地方?

php - Phpmyadmin 中的 MySQL 数据库不会更新并且不显示任何错误

php - undefined variable $submit

php - 使用 php 无法正确显示月份

mysql - 如何从 Mysql 查询中获取下一行值

php - Yii框架空白页问题多次

yii CGridView 数据提供者和过滤器