dqb77047 2013-07-18 10:49
浏览 45
已采纳

Yii模型CGridView包括具有Yii用户配置文件字段数据的列

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?

  • 写回答

1条回答 默认 最新

  • douhan9191 2013-07-18 13:04
    关注

    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'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?