Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

php - Yii framework: Using data from related Active Record models for searching

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

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

I have order table and user table

Order table and User table has One to many Relation.

User has many orders and order has exactly one user.

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

Order table has 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(
        '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'
        ),
       ),
));

Error message returned

enter image description here

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

enter image description here

Thanks in advance for any help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Following are the two tables i have. Order and User

enter image description here

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.

enter image description here

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

enter image description here

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

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.

Other thing is the order of the elements

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

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

i updated the order admin with the

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...