Ddd
(Daanvandugteren)
1
I’m looking to enable search criteria in a model for a related module’s model.
Here’s an example to explain what I mean:
-
I have a ‘Product’ model which contains ‘user_id’.
-
In Yii-user I created a Custom Profile Field called ‘brand’.
-
I have a CGridView (with Search/Filter functionality) in my Product model’s index view which lists all products.
-
I want to add the ‘brand’ profile field to the CGridView columns allowing people to filter and search by ‘brand’.
I added the following code to my Product model relations:
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
);
}
And then adding this to the Product model’s search criteria:
$criteria->compare('user.brand', $this->user->profile->brand, true);
This however yields no positive results.
I’m not sure if I’m going the right direction here. Anyone know how to do this?
Ddd
(Daanvandugteren)
2
If anyone knows how to do this or requires more information please let me know
jayant
(Codesutras)
3
You have to do something like this in your product model.
$criteria = new CDbCriteria;
$criteria->with = array( 'author' );
...
$criteria->compare( 'author.username', $this->author_search, true );
Also you can refer this wikki article
Ddd
(Daanvandugteren)
4
I read the article but didn’t get it to work with the profile fields of the yii-user module.
In this example getting the profile field for yii-user I’d have to use
Yii::app()->getModule('user')->user()->profile->brand
However I don’t know how to implement this so it fulfills the bullet points I named in the OP.
Ddd
(Daanvandugteren)
5
Using
$criteria->compare( 'user.brand', $this->user_search, true );
doesn’t appear to work when it comes to custom profile fields?