【问题标题】:Yii framework: Using data from related Active Record models for searchingYii 框架:使用来自相关 Active Record 模型的数据进行搜索
【发布时间】:2012-01-27 09:54:56
【问题描述】:

Yii 1.1 应用程序开发 Cookbook 解释了一种使用来自相关 Active Record 模型的数据来搜索相关模型的方法。此方法在第 193 页和第 194 页中进行了说明。我已尝试将此方法集成到我的应用程序中,但它不起作用。谁能解释一下这个功能在 Yii 框架版本 1.1.8 中是否仍然可用

在这个位置,我还可以找到用于搜索数据表单相关活动记录模型的 cmets。但它也不起作用。 http://www.yiiframework.com/doc/api/1.1/CDbCriteria

我有订单表和用户表

订单表和用户表有一对多关系。

用户有很多订单,而订单只有一个用户。

所以,我正在编辑 CDbCriterial 以在订单表搜索条目中包含用户表名称和电子邮件字段。

订单表有如下关系

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'comments' => array(self::HAS_MANY, 'Comment', 'order_id'),
        'user' => array(self::BELONGS_TO, 'User', 'user_id'),
        'orderstatus' => array(self::BELONGS_TO, 'Orderstatus', 'orderstatus_id'),
        'commentCount' => array(self::STAT, 'Comment' , 'order_id')
    );
}

这是包含用户表名称字段的搜索/过滤条件

public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('order_create_date',$this->order_create_date,true);
        $criteria->compare('price',$this->price,true);
        $criteria->compare('bank_account_number',$this->bank_account_number,true);
        $criteria->compare('hardwaredetail_Id',$this->hardwaredetail_Id);
        $criteria->compare('user_id',$this->user_id);
        $criteria->compare('order_update_date',$this->order_update_date,true);
        $criteria->compare('is_received',$this->is_received);
        $criteria->compare('order_received_date',$this->order_received_date,true);
        $criteria->compare('is_notify_by_email',$this->is_notify_by_email);
        $criteria->compare('warehouse_offered_price',$this->warehouse_offered_price,true);
        $criteria->compare('warehouse_offered_price_date',$this->warehouse_offered_price_date,true);
        $criteria->compare('orderstatus_id',$this->orderstatus_id);
        $criteria->together = true; 
        $criteria->with = array('user');
        $criteria->compare('user.name',$this->user,true);
        //$criteria->compare('user.name',$this->user->name);
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

并编辑订单管理页面以显示名称字段如下

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'order-grid',
    'dataProvider'=>$model->search(),
    //'filter'=>$model,
    'columns'=>array(
        'id',
        'order_create_date',
        'price',
        'bank_account_number',
        array(
            'name'=>'user',
            'value'=>'$data->user->name'
        ),
       ),
));

返回错误信息

通过应用 thaddeusmt 提供的解决方案解决了 id 列歧义问题后,我遇到了以下错误消息。

提前感谢您的帮助

【问题讨论】:

  • 你能发布一些你的代码吗?至少样品..
  • 我已按照您的要求添加了代码
  • 您收到任何错误信息吗?并且你添加了“public $user;”在第一行排序类模型?
  • @a.v 是的,我收到以下错误消息/我已经用错误消息更新了问题

标签: php yii


【解决方案1】:

我可以找到答案。这就是我所做的一切。

以下是我的两张桌子。订单和用户

我有订单/管理页面,默认生成的代码只提供搜索订单表数据。我想在搜索条件中关联用户表名称字段。

这是搜索页面的初始外观。

我想将其他表中的用户名整合到此搜索中。那么最终的样子如下。

所以这些是我做的步骤。

Order 模型中的第一个我有以下关系

    public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(

                'user' => array(self::BELONGS_TO, 'User', 'user_id'),


            );
        }
This is generated by Yii framework , i did nothing :)

Then , i changed the search method as follows

public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;


        $criteria->compare('t.order_create_date',$this->order_create_date,true);
        $criteria->compare('t.price',$this->price,true);
        $criteria->compare('t.bank_account_number',$this->bank_account_number,true);
        $criteria->compare('t.hardwaredetail_Id',$this->hardwaredetail_Id);
        //$criteria->compare('user_id',$this->user_id);

        $criteria->compare('t.order_update_date',$this->order_update_date,true);
        $criteria->compare('t.is_received',$this->is_received);
        $criteria->compare('t.order_received_date',$this->order_received_date,true);
        $criteria->compare('t.is_notify_by_email',$this->is_notify_by_email);
        $criteria->compare('t.warehouse_offered_price',$this->warehouse_offered_price,true);
        $criteria->compare('t.warehouse_offered_price_date',$this->warehouse_offered_price_date,true);
        $criteria->compare('t.orderstatus_id',$this->orderstatus_id);
        $criteria->together = true; 
        $criteria->compare('t.id',$this->id,true);
        $criteria->with = array('user');
        $criteria->compare('name',$this->user,true,"OR");
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

如果您的订单表主键字段都有保存名称,则将 t 放在 t 的前面很重要。就我而言,它是 id 和 id,所以我不得不放 t。

还有一点就是元素的顺序

$criterial->togeter = true;应该在关系元素之前。

然后你更新到订单表中的规则方法。我将用户归档和名称归档添加到安全属性。

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            //array(' orderstatus_id', 'required'),
            array('hardwaredetail_Id, user_id, is_received, is_notify_by_email, orderstatus_id', 'numerical', 'integerOnly'=>true),
            array('price, warehouse_offered_price', 'length', 'max'=>10),
            array('bank_account_number', 'length', 'max'=>100),
            array('order_create_date, order_update_date, order_received_date, warehouse_offered_price_date, user,name', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, order_create_date, price, bank_account_number, hardwaredetail_Id, user_id, order_update_date, is_received, order_received_date, is_notify_by_email, warehouse_offered_price, warehouse_offered_price_date, orderstatus_id', 'safe', 'on'=>'search'),
        );
    }

最后更新您的 UI 代码。

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'order-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'order_create_date',
        'price',
        'bank_account_number',
        array(
            'name'=>'user',
            'value'=>'$data->user->name'
        )
)); ?>

我用

更新了订单管理员
array(
                'name'=>'user',
                'value'=>'$data->user->name'
            )

这就是我所做的,它对我有用。问我是否需要任何帮助。感谢每一位关注这个问题的人。

【讨论】:

  • 我的立场是正确的,我认为您不能使用 user 作为列名 - 我认为 $model-&gt;attributes 不会为 search() 方法设置数据。每天学习新东西!
  • 添加到安全属性的用户字段和名称字段可以改为添加到安全搜索规则中。
  • 快速搜索 (ajax) 不起作用。奇怪但真实的高级搜索有效。为什么?
【解决方案2】:

看起来userorder 都有名为id 的列。并且您的条件在 WHERE 子句中都使用了它们,这会给出“模棱两可”的 mySql 错误消息。

当使用with 条件(对表执行 SQL JOIN)时,如果您的两个表具有相同名称的列,您需要在条件上使用 mySql“点”前缀,如下所示:

$criteria->compare('t.id',$this->id); // the default table prefix in Yii is "t"

当我使用 $criteria-&gt;with 加入表时,我会为所有列名添加前缀(在我的 comparecondition 标准等中),如下所示:

$criteria->compare('t.id',$this->id); // t
$criteria->compare('t.order_create_date',$this->order_create_date,true); // t
$criteria->with = array('user');
$criteria->compare('user.name',$this->user_id,true); // user (the filter will set user_id)

您的网格视图需要如下所示:

array(
  'name'=>'user_id',
  'header'=>'User',
  'sortable'=>false,
  'value'=>'$data->user->name'
),

此外,正如您在编辑中指出的那样,我认为还有一个更大的问题:

您的搜索功能设置如下:user.name',$this-&gt;user - 但$this-&gt;user 将通过关系返回当前用户对象,而不是搜索条件。列过滤器将设置 user_id, 属性。

编辑:不,您可以$this-&gt;user 作为您的列名,只要您将其设置为safe 属性即可。

这里更详细地展示了我解决这个问题的方法:

Search in BELONGS_TO model column with CGridView, Yii

不过,排序不适用于过滤 - 只是过滤。

这里是 Yii 论坛的好帖子,也可能给你更多的线索:

http://www.yiiframework.com/forum/index.php?/topic/9083-search-filter-of-a-relations-field-through-cgridview/

遗憾的是,对关系进行排序和过滤 CGridViews 并不是真正的默认功能。您可以使用user.name 等列名轻松显示相关信息,但不会排序或过滤。祝你好运!

【讨论】:

  • 应用您提到的解决方案后。我收到新错误。我已经用新的错误消息更新了问题。
  • 感谢 thaddessmt 的回答,我可以解决问题。您的解决方案帮助很大。
  • 按照你的方法我收到错误未定义变量:数据
  • 我想不通,我收到错误尝试获取非对象的属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多