Table Relation

whats is wrong with my model, i got this error


Active record "Tindakan" is trying to select an invalid column "idKategori.nama_kategori". Note, the column must exist in the table or be an expression with alias.

my search model


public function search2()

 {

     $criteria=new CDbCriteria;

     $criteria->select='

       t.id_tindakan, 

       t.nama_tindakan, 

       t.id_kategori,

       t.harga,

       t.keterangan, 

       idKategori.nama_kategori';


       $criteria->with=array('idKategori');

       $criteria->compare('id_tindakan',$this->id_tindakan);

       $criteria->compare('nama_tindakan',$this->nama_tindakan,true);

       $criteria->compare('id_kategori',$this->id_kategori);

	$criteria->compare('harga',$this->harga);

	$criteria->compare('keterangan',$this->keterangan,true);

       $criteria->compare('idKategori.nama_kategori',$this->nama_kategori,true);


       return new CActiveDataProvider(get_class($this), array(

         'criteria'=>$criteria,

         'sort'=>array(

           'attributes'=>array(

              'nama_kategori'=>array(

                 'asc'=>'idKategori.nama_kategori',

                 'desc'=>'idKategori.nama_kategori DESC',

              ),

              

              '*',

          ),

        ),

    ));

 }

Relation


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(

			'tasks' => array(self::HAS_MANY, 'Task', 'id_tindakan'),

			'idKategori' => array(self::BELONGS_TO, 'Kategori', 'id_kategori'),

		);

	}

Thanks before

You should add


$criteria->together = true

Dear Friend

We have to declare the nama_kategori as virtual property in the current model(Tindakan).

Then we have to make it safe on search in rules.




public $nama_kategori;


public function rules()

	{

		..................................other rules .....................


			array('nama_kategori', 'safe', 'on'=>'search'),

		);


	}


public function search2()

 {

     $criteria=new CDbCriteria;

     $criteria->select='

       t.id_tindakan, 

       t.nama_tindakan, 

       t.id_kategori,

       t.harga,

       t.keterangan, 

       idKategori.nama_kategori AS nama_kategori'; //mapping the column value to a virtual property.


       $criteria->with=array('idKategori');

       $criteria->compare('id_tindakan',$this->id_tindakan);

       $criteria->compare('nama_tindakan',$this->nama_tindakan,true);

       $criteria->compare('id_kategori',$this->id_kategori);

        $criteria->compare('harga',$this->harga);

        $criteria->compare('keterangan',$this->keterangan,true);

       $criteria->compare('idKategori.nama_kategori',$this->nama_kategori,true); //Now we have declared the virtual property ;it is not going to throw errors


       return new CActiveDataProvider(get_class($this), array(

         'criteria'=>$criteria,

         'sort'=>array(

           'attributes'=>array(

              'nama_kategori'=>array(

                 // It is ok to leave it empty here without any array values.

              ),


              

              '*',

          ),

        ),

    ));

 }







Now in admin.php add the column




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'tindakan-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'id',

                  'nama_kategori',  //Just adding the virtual property.

           ..... other columns to follow....



Now we have made the relational column sortable and filterable as well.

Regards.

It’s work, thanks bro

This is an interesting alternative to add searcheable and filterable colums.

However I do suggest that you have a look at my RelatedSearchBehavior extension which I think makes it easier to do this (in another way, not using virtual attributes created by the query, but defining the virtual attributes in PHP through the extension).

ok i’ll try it :)