Search By Related Field

Hi,

I know there are plenty of topics on this but I searched&tried so many and it is still not working.

I tried those for example:

  1. http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

  2. http://www.yiiframework.com/forum/index.php/topic/35027-cgridview-search-with-relation/page__p__168484__hl__+earch+related+field#entry168484

I have tables: Team and Worker. Any worker can be assigned to a Team. So at the Workers Manager I want to search Workers also by Team name.

I got the column etc. but when I type part of team name - search starts but the written text dissappears and search doesn’t care about the field. I checked the AJAX call with Firebug and there is a field called teamName (I added public field to my Worker model class). But when I print_r criteria in my search method - there is no condition.

How is that possible? How can I perform the searching by related field?

How about trying my RelatedSearchBehavior extension.

It does work, thanks. I just wanted to do it the way the guide provide - so I was surprised it is not working (altough people say it does).

One question @le_top - I noticed that it’s working only with default search method. Is it possible to define for example relatedSearch() method and do the search there?

The other thing - is it possible to pass constant ‘NOT LIKE’ parameter to the search programmatically?

Hi

If you want to add a constant ‘NOT LIKE’ comparison on a relation with the RelatedSearchBehavior you could define a specific related attribtute for that and set the value of the attribute to “<>SEARCH_TEXT”.

Hello!

I have problem with model search & cdbcriteria. (some parts in hungarian lang.)

  1. Jqcalendar model, this have HAS_MANY "jarmuvek" relation ( Foglaltjarmu[] ) One jqcalendar record have many Foglaltjarmu rows

@property Foglaltjarmu[] $jarmuvek

‘jarmuvek’ => array(self::HAS_MANY, ‘Foglaltjarmu’, ‘FuvarID’),

  1. Foglaltjarmu[] model, this have BELONGS_TO jarmu relation ( Flotta model ) One Foglaltjarmu record have one jarmu record.

@property Flotta $jarmu

‘jarmu’ => array(self::BELONGS_TO, ‘Flotta’, ‘jarmuID’),

How do i search jarmu.rendszam field in Jqcalendar model? It possible? ex. group by?

Manual sql query: select * from jqcalendar left join foglaltjarmu on foglaltjarmu.FuvarID = jqcalendar.Id left join Flotta on Flotta.id = foglaltjarmu.jarmuID where Flotta.rendszam = ‘HOS-108’ group by …

This does not work for me.


	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

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

		$criteria->compare('jarmu.rendszam',$this->rendszam);

Thanks.

Edit: Solved.


'jarmuvek' => array(self::HAS_MANY,'Foglaltjarmu','FuvarID','with'=>'jarmu'),