Yii 框架:在数据提供者中加入表(或其他 SQL)? [英] Yii Framework : Join table (or other SQL) in data provider?

查看:25
本文介绍了Yii 框架:在数据提供者中加入表(或其他 SQL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我在类别"模型中使用了 Gii Crud Generator,我想修改管理表单.

I have used Gii Crud Generator with my "Category" model, and I want to modify the admin form.

我查看protected/views/Category/admin.php",

I look inside "protected/views/Category/admin.php,

我发现表格是由小部件呈现的('zii.widgets.grid.CGridView'),并且它使用一个数据提供者来处理它的数据.

I found the table is render by a widget('zii.widgets.grid.CGridView'), and it using a data Provider for it's data.

我想我可以在数据提供程序中找到一些输入 SQL 查询的位置,但我不明白它是如何工作的.

I suppose I can find some where to input the SQL query in the data Provider, but I don't understand about how's it works.

这些是Model->relations()中的代码,但我不知道接下来要做什么.

these is the code In the Model->relations(), but I don't know what to do next.

public function relations(){
    return array(
        'cateLang' => array(self::HAS_MANY, 'CategoryLang', 'cate_id')
    );
}

生成数据提供者的位置:

where the data provider is generated :

public function search(){

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('status',$this->status,true);
    $criteria->compare('createDate',$this->createDate,true);
    $criteria->compare('updateDate',$this->updateDate,true);
    $criteria->compare('remark',$this->remark,true);

    return new CActiveDataProvider($this->with('cateLang'), array(
        'criteria'=>$criteria,
    ));
}

目标

我想在protected/views/Category/admin.php"表中再添加两列,

I want to add two more columns at the table of "protected/views/Category/admin.php,

这将显示法语标题 &行的英文标题.

which will show French Title & English Title of the row.

要在 SQL 中获取数据,它将是:

To get data in SQL, it will be :

SELECT 
    cate.id,
    lang1.name as "FrenchTitle",
    lang2.name as "EnglishTitle",
    cate.updateDate,
    cate.createDate,
    cate.remark 
FROM `category` cate
LEFT JOIN `categorylang` lang1
    ON `lang1`.`cate_id` = `cate`.id 
    AND `lang1`.`lang_id`= 1
LEFT JOIN `categorylang` lang2
    ON `lang2`.`cate_id` = `cate`.id 
    AND `lang2`.`lang_id`= 2
WHERE cate.status = 'live'

如果我可以用 data Provider 完成,CGridView 参数可能是这样的:

If I can done with data Provider, the CGridView parameter may be like this :

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'category-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'FrenchTitle',
        'EnglishTitle',
        'createDate',
        'updateDate',
        'remark',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); 

推荐答案

您可以尝试以下操作:

public function search(){

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('status',$this->status,true);
    $criteria->compare('createDate',$this->createDate,true);
    $criteria->compare('updateDate',$this->updateDate,true);
    $criteria->compare('remark',$this->remark,true);
    $criteria->with = array('cateLang' => array(
        'condition' => 'cateLang.id = 1 OR cateLang.id = 2',
        'order' => 'cateLang.id ASC'
    ));

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));

}

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'category-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        array(
            'name' => 'FrenchTitle'
            'value' => '(isset($data->cateLang[0])) ? $data->cateLang[0]->name : "no Title"',
        ),
        array(
            'name' => 'EnglishTitle'
            'value' => '(isset($data->cateLang[1])) ? $data->cateLang[1]->name : "no Title"',
        ),
        'createDate',
        'updateDate',
        'remark',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); 

在搜索中,我指定我只需要 ID 为 1 或 2 的 cateLang 对象,然后在 cgridview 中显示一个关系对象.

In the search I specify that I want only cateLang object with the id 1 or 2 and then in the cgridview I display a relational object.

这篇关于Yii 框架:在数据提供者中加入表(或其他 SQL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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