Yii 框架:使用来自相关 Active Record 模型的数据进行搜索

2022-01-04 00:00:00 php yii

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

Yii 1.1 application development Cookbook explain a method for using data from related Active Record Models for searching the related models as well. This method is explained in page number 193 and 194. i have tried to integrate this method in to my application but it does not work. could anybody explain me whether this feature is still available in Yii framework version 1.1.8

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

At this location also i could find comments for searching data form related active record models. But it also does not work. http://www.yiiframework.com/doc/api/1.1/CDbCriteria

我有订单表和用户表

订单表和用户表是一对多的关系.

Order table and User table has One to many Relation.

用户有很多订单,订单只有一个用户.

User has many orders and order has exactly one user.

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

So , i am editing following CDbCriterial to include user tables name and email field in to Order tables search entries.

订单表有如下关系

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')
    );
}

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

This is the search/filter conditions with user table's name filed included

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,
        ));
    }

和订单管理页面被编辑以显示如下提交的名称

and Order admin page is edited to display the name filed as follows

<?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 列歧义问题后,我遇到了以下错误消息.

After solving the id column ambiguity problem by applying the solution that thaddeusmt gave i have faced with the following error message.

在此先感谢您的帮助

推荐答案

我能找到答案.这就是我所做的.

I could find the answer. This is all i did.

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

Following are the two tables i have. Order and User

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

I have Order/Admin page, Default generated code provide searching order table data only. i want to relate user tables name field in the search criteria.

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

This is the initial look of the search page.

我想将从其他表中归档的用户名集成到此搜索中.那么最终的外观将如下所示.

I want to integrated user name filed from other table in to this search. then final look will be as follows.

所以这些是我做的步骤.

so these are the steps i did.

首先在 Order 模型中我有以下关系

first in the Order model i have following relations

    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.

it is important to put t in-front of the t if your Order table primary key field if both have save name. in my case it is id and id, so i had to put t.

其他的是元素的顺序

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

$criterial->togeter = true; should come before the relational elements.

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

then u updated to rules method in Order table. i added user filed and name filed to safe attributes.

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 代码.

Finally update your UI code.

<?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'
            )

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

That is what i did and it worked for me. ask me if you need any help. Thanks every one looking in to this issue.

相关文章