简体   繁体   中英

Yii Model CGridView including column with Yii-User Profile Field data

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 (which is a custom Profile Field made with Yii-User) to the CGridView columns allowing people to filter and search the grid view by 'brand'.

I added the following code to my Product model relations:

public function relations()
{
            Yii::import('application.modules.user.models.*');
    Yii::app()->getModule('user');
    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?

Why you put the below lines in 'relations()' function?

Yii::import('application.modules.user.models.*');
Yii::app()->getModule('user');

You need put 'brand' how a public or private attribute on 'Product' model. This attribute is not persistent, only use for the get the filter form value.

Then use like this:

$criteria->compare('user.brand', $this->brand, false);

And is possible that you need put 'brand' attribute on the 'rules()', on the array() with 'on'=>'search'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM