how to search across relations

Hello,

I have a prjmaster and prjdetail table that has a NOTES field that I want to search.

In the prjmaster gridview (admin.php) I coded this>




array(            

       'name'=>'search_location',

       'value'=>'$data->prjdetail->NOTES',

       'header'=>'Notes',

     ),



In my model search function




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

if(strlen($this->search_location))

  $criteria->addSearchCondition('prjdetail.NOTES',$this->search_location,true);



In my model I have defined search_location and added it as safe.

I am getting errors.




Trying to get property of non-object

Source File


C:\wamp\www\yii\base\CComponent.php(616) : eval()'d code(1)


No source code available.



Any ideas? Thanks




return array(

	'prjdetail' => array(self::HAS_MANY, 'Prjdetail', 'ID'),

);



I changed admin.php to




array(            

                  'name'=>'search_location',

                  'value'=>'$data->search_location',

                  'header'=>'Notes',

                ),



But it still fails

I think that this cannot work.

If your relation is a many-many, how can you show a value in a column? You can have more than one value.

That’s why you get Trying to get property of non-object: the result of $data->prjdetail is an array, not an object.

The search fails because Yii uses different alias than prjdetail.

For you is better to add the search field and add a condition like:


 $criteria->addCondition("id IN (SELECT project from notes where location like '%{$this->search_location}%')",);

Do I have my relation coded wrong?

I want a one to many relation.

The relation is correct, it means that a project has many projdetails.

The question is that in a grid of project, you have many details, and so, wich one of the many detail you will display in this column?

ok,

I added this, but get error in mysql.




$criteria->addCondition("prjmaster->ID IN (SELECT MasterId from Prjdetail where NOTES like '%{$this->search_location}%')");



It is working now, thanks

had to change prjmaster->ID

to ID