duannaikuang1301 2013-11-29 12:10
浏览 24
已采纳

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

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,

   ));

}

This is my 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 '

        ),
 ));

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

and ExiProducts are

UserId,
ProductId

Updated my code

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;


}
  • 写回答

3条回答 默认 最新

  • dsztc99732 2013-11-29 12:31
    关注

    view of 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' => array($this,'gridCreateduser')
            )
         ),
    ));
    

    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()

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

    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
           )
        ),
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动