【问题标题】:How to display data from related tables in CGridview in yii如何在yii的CGridview中显示相关表中的数据
【发布时间】:2013-12-15 15:15:04
【问题描述】:

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


}

【问题讨论】:

    标签: php yii cgridview


    【解决方案1】:

    网格视图视图

    $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 =&gt; array($this,'gridCreatedUser'),这意味着网格视图将在其控制器中搜索函数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;
    }
    

    不,这会将该列名称的所需值发送到网格视图。

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

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

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

    $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】:

    这很容易。

    你所要做的就是在模型文件 ExiProducts 中写一个关系

      public function relations() {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                'user' => array(self::BELONGS_TO, 'users', 'UserId'),
            );
        }
    

    你可以在网格视图中使用它

    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'users-grid',
    
        'dataProvider'=>$dataProviderObj,
    'columns'=>array(
    
            'productName',
            'productType',
            'productBrand',
            array(
        'header'=>'User Name', 
        'value'=>'CHtml::encode($data->user->Username)',//This will use the relationship and get all the details of the paticular user from users table
            ),
    
        ),
    ));
    

    【讨论】:

    • 如果你已经创建了关系,你可以在列定义中使用 user.UserName。
    【解决方案3】:

    通过编辑模型页面中的关系()来在用户表中添加关系,如图所示

    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'users', 'UserId'),
        );
    }
    

    在你的 view.php 中,

    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'users-grid',
    
        'dataProvider'=>$dataProviderObj,
        'columns'=>array(
    
            'productName',
            'productType',
            'productBrand',
            'description',
             array(
                'name'=>'User Name', 
                'value'=>$model->->user->Username,
    
            ),       
    
        ),
    ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多