【问题标题】:How to use Left Join sql query result into a model gridview in yii如何在 yii 中使用 Left Join sql 查询结果到模型 gridview
【发布时间】:2013-12-16 09:41:17
【问题描述】:

这是代码,

$model=new Tblvehicle;

$sql="Select tblvehicle.serial_no, tblmodel.description, tbluser.user_name, tbluser.user_code "
    . "From tblvehicle"
    . " left join tblmodel "
    . "on tblvehicle.model_code= tblmodel.model_code"
    . " left join tbluser"
    . " on tblvehicle.user_code=tbluser.user_code"; 
$query= Yii::app()->db->createCommand($sql)->queryScalar();
$dataProvider = new CSqlDataProvider($sql, array('totalItemCount'=>$query));

$this->render('index',array(
   'model'=>$model,'dataProvider'=> $dataProvider));

这是我不完整的Tblvehicle 网格视图 -

$this->widget('zii.widgets.grid.CGridView', array(
              'id'=>'tblvehiclegrid',
              'dataProvider'=>$dataProvider,
              'filter'=>$model,
              'columns'=>array(

现在我如何显示这 4 列 serial_no, description, user_name,user_code?因为这些来自不同的表,我不知道如何添加它们。

'columns'=>array(

也许我尝试这样做的方式也是错误的。所以请在这方面需要一些帮助,我对 Yii 很陌生。

【问题讨论】:

    标签: php sql gridview yii


    【解决方案1】:

    这是必要的控制器代码

    $criteria=new CDbCriteria();
    $dataProvider=new CActiveDataProvider('Tblvehicle', array(
                                                        'criteria'=>$criteria,
                                                        'pagination'=>array(
                                                            'pageSize'=>5,
                                                            ),
                                            ));
    
    return $dataProvider;
    
    $this->render('index',array(
    'model'=>$model,'dataProvider'=> $dataProvider));
    

    将以下行添加到 Tblvehicle 模型的关系部分,使其看起来像这样,仅使用 4 个关系中的两个,在代码 cmets 中阅读原因

    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(
    
    
            'rl_model'=>array(self::BELONGS_TO, 'Tblmodel', 'model_code'),//presuming that model_code is the primary key of this table if not use the two below
            'rl_user'=>array(self::BELONGS_TO, 'Tbluser', 'user_code'),//presuming that user_code is the primary key of this table if not use the two below
    
            'rl_model' => array(self::BELONGS_TO, 'Tblmodel', '', 'foreignKey' => array('model_code'=>'model_code')), //Non Primary field
            'rl_user' => array(self::BELONGS_TO, 'Tbluser', '', 'foreignKey' => array('user_code'=>'user_code')), //Non Primary field
        );
    }
    

    然后在视图中使用这个

        'columns'=>array(
                        'serial_no',
                        'subject',
                        array(
                            'name'=>'Description Code',
                            'value'=>'$data->rl_model->description',
                        ),
    
                        array(
                            'name'=>'Username',
                            'value'=>'$data->rl_user->user_name',
                        ),
                        array(
                            'name'=>'User Code',
                            'value'=>'$data->rl_user->user_code',
                        ),
    
                    ),
    

    基本上,我们不必编写 mysql 语句和使用连接,而是可以使用关系有效地创建连接。然后可以随时引用此字段,如上所示。

    让我知道你过得怎么样。

    【讨论】:

    • 非常感谢,先生为此发疯了。它现在正在工作。只有一个小的修正。我认为我们需要使用“标题”而不是“名称”。
    • @Farhad 没问题。我使用name 的原因实际上是因为您无法在没有更多摆弄的情况下对自定义列进行排序。这篇文章yiiframework.com/forum/index.php/topic/… 将向您展示如何在需要时对它们进行排序。但是您在您的情况下是正确的header 是这样。很高兴我能帮上忙。
    猜你喜欢
    • 2016-09-10
    • 1970-01-01
    • 2015-01-10
    • 2015-02-24
    • 2017-11-16
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 2019-03-07
    相关资源
    最近更新 更多