0

Background

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

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

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

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.

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

Target

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.

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'

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',
        ),
    ),
)); 
Zitty Yam
  • 135
  • 1
  • 12

1 Answers1

0

You could try the following:

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

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.

darkheir
  • 8,844
  • 6
  • 45
  • 66
  • I wonder can I change "$data->cateLang[0]" into something like "$data->findByAttributes(array('lang_id'=>'1'))" ? – Zitty Yam Aug 07 '14 at 08:48
  • hum $data is of type Category so I don't think it will work. May be: CategoryLang::model()->findByAttributes((array('lang_id'=>'1', 'cate_id' => $data->id))->name. But I don't think this is the best move – darkheir Aug 07 '14 at 09:09
  • Now my work is fine here. But just for learning, will it go wrong if the entry of 'lang_id=1' is missing ? Will it display data of 'EnglishTitle' in column of 'FrenchTitle' ? – Zitty Yam Aug 07 '14 at 09:28
  • If you use the solution in my answer yeah you'll have this problem. – darkheir Aug 07 '14 at 09:30