微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Yii框架:使用相关Active Record模型中的数据进行搜索

Yii 1.1应用程序开发Cookbook解释了使用相关活动记录模型中的数据来搜索相关模型的方法.第193页和第194页解释了这种方法.我试图将此方法集成到我的应用程序中,但它不起作用.任何人都可以解释我Yii框架版本1.1.8中是否仍然可以使用此功能

在这个位置我也可以找到搜索相关活动记录模型的数据表单的注释.但它也行不通.
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,'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 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,);
        }
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',true);
        $criteria->compare('t.price',true);
        $criteria->compare('t.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',true);
        $criteria->compare('t.is_received',$this->is_received);
        $criteria->compare('t.order_received_date',true);
        $criteria->compare('t.is_notify_by_email',$this->is_notify_by_email);
        $criteria->compare('t.warehouse_offered_price',true);
        $criteria->compare('t.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',true,"OR");
        return new CActiveDataProvider($this,));
    }

如果您的Order表主键字段(如果两者都具有保存名称),则将t放在t前面是很重要的.在我的情况下它是id和id,所以我不得不把t.

其他的是元素的顺序

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

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

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','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,warehouse_offered_price,'safe','on'=>'search'),);
    }

最后更新您的UI代码.

<?PHP $this->widget('zii.widgets.grid.CGridView','filter'=>$model,'value'=>'$data->user->name'
        )
)); ?>

我更新了订单管理员

array(
                'name'=>'user','value'=>'$data->user->name'
            )

这就是我所做的,它对我有用.问你是否需要帮助.感谢每个人都在关注这个问题.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐