Yii框架:使用相关Active Record模型中的数据进行搜索 [英] Yii framework: Using data from related Active Record models for searching

查看:63
本文介绍了Yii框架:使用相关Active Record模型中的数据进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Yii 1.1应用程序开发手册解释了一种使用相关Active Record模型中的数据搜索相关模型的方法.在第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.

在订购模型中,我首先具有以下关系

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

如果您的Order表主键字段都具有保存名称,则在 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.

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

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.

这篇关于Yii框架:使用相关Active Record模型中的数据进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆