Multiple Related Tabels Search Criteria

Hello!

I have a problem again. :)

I’m working with multiple related tabels and I want to make all the fields that I use in my CGridView searchable. I’ve managed to make it work with one field but as it looks like I’m doing something wrong with criteria. Here is my code:

model:


//define variables

public $Lok_search;

public $Dej_search;

public $Servis_search;

...


//rules for variables

public function rules()

{

return array(

...

	array('..., Lok_search, Dej_search, Servis_search', 'safe', 'on'=>'search'),

);

}




//relations

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(

			'idServiser0' => array(self::BELONGS_TO, 'Servis', 'idServiser'),

			'idSkrbnik0' => array(self::BELONGS_TO, 'Uporabnik', 'idSkrbnik'),

			'idZadolzen0' => array(self::BELONGS_TO, 'Uporabnik', 'idZadolzen'),

			'idLokacija0' => array(self::BELONGS_TO, 'Lokacija', 'idLokacija'),

			'idDejavnost0' => array(self::BELONGS_TO, 'Dejavnost', 'idDejavnost'),

			'posegs' => array(self::HAS_MANY, 'Poseg', 'Inventarna'),

		);

	}

...

//search criteria

$criteria=new CDbCriteria;

$criteria->with = array('idLokacija0', 'idDejavnost0', 'idServiser0');

$criteria->together= true;

$criteria->compare('idLokacija0.NazivLokacija', $this->Lok_search,true);

$criteria->compare('idServiser0.Naziv', $this->Servis_search,true);

$criteria->compare('idDejavnost0.NazivDejavnost', $this->Dej_search,true);



view:


array( 'name'=>'Lok_search', 'value'=>'$data->idLokacija0->NazivLokacija'),

	array( 'name'=>'Dej_search', 'value'=>'$data->idDejavnost0->NazivDejavnost'),

	array( 'name'=>'Servis_search', 'value'=>'$data->idServiser0->Naziv'),



With this code i get error:"Trying to get property of non-object". With all the reading I did my only idea is that I get some empty fields when searching. That is correct, because Serviser can be empty. How can I solve this problem? Is there any way I can do this? I hope I was clear enough.

You could have identified the code that produced the error by reading the stack trace carefully … but I guess the error is in the displaying phase, not in the searching phase.

Try the following:




array( 'name'=>'Servis_search', 'value'=>'$data->idServiser0 ? $data->idServiser0->Naziv : null'),



Great! Thank you. And you were right, I was looking into stack trace more carefully. Thanks again!