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

php – 使用CDbCriteria和CActiveDataProvider进行连接的Yii

这个问题似乎弹出了很多,但没有一个答案帮助我解决了我的问题.

摘要

>我正在使用Yii创建一个应用程序;
>我有三张桌子,我正在尝试加入并过滤其中两张;
>我正在尝试使用CDbCriteria和CActiveDataProvider来进行连接和过滤;
>我有所有表的模型,但是当我尝试加入它们时,我得到一个sql错误.

我为要加入和过滤的表创建了一个模型.

记录

class Record extends CActiveRecord {
    public $owner;
    ...
    public function rules() {
        return array(
            array('given_name, family_name, dob, gender', 'required'),
            array('qr_id, site_id', 'numerical', 'integerOnly' => true),
            array('given_name, family_name, madin_name', 'length', 'max' => 100),
            array('country_of_birth, country_of_death, title', 'length', 'max' => 45),
            array('gender', 'length', 'max' => 5),
            array('dod, profile, epitaph', 'safe'),
            array('id, qr_id, given_name, family_name, madin_name, dob, dod, country_of_birth, country_of_death, gender, owner', 'safe', 'on' => 'search'),
    );
}

    ...
    public function relations() {
        return array(
            'families_left'  => array(self::HAS_MANY, 'Family', 'record_left_id'),
            'families_right' => array(self::HAS_MANY, 'Family', 'record_right_id'),
            'headstones'     => array(self::HAS_MANY, 'Headstone', 'record_id'),
            'other_names'     => array(self::HAS_MANY, 'OtherName', 'record_id'),
            'users'          => array(self::MANY_MANY, 'Users', 'record_owner(record_id, user_id)'),
            'record_owner'   => array(self::HAS_MANY, 'RecordOwner', 'record_id'),
        );
    }
    ...
}

RecordOwner

class RecordOwner extends CActiveRecord {
    ...
    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();
    }
    ...
}

问题

我有更新搜索在record_owner上添加一个条件到CDbCriteria,我在record_owner.user_id上添加一个比较但现在得到sql错误.

搜索()

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('qr_id', $this->qr_id);
    $criteria->compare('given_name', $this->given_name, true);
    $criteria->compare('family_name', $this->family_name, true);
    $criteria->compare('madin_name', $this->madin_name, true);
    $criteria->compare('dob', $this->dob, true);
    $criteria->compare('dod', $this->dod, true);
    $criteria->compare('country_of_birth', $this->country_of_birth, true);
    $criteria->compare('country_of_death', $this->country_of_death, true);
    $criteria->compare('gender', $this->gender, true);
    $criteria->compare('title', $this->title, true);

    $criteria->with = array('record_owner');
    $criteria->compare( 'record_owner.user_id', $this->owner, true );

    return new CActiveDataProvider(
        $this,
        array(
             'criteria'   => $criteria,
             'pagination' => array(
                 'pageSize' => Yii::app()->params['pageSize'],
             )
        )
    );
}

sql错误

CDbCommand Failed to execute the sql statement: sqlSTATE[42S22]: Column not found: 1054 UnkNown column 'record_owner.user_id' in 'where clause'. The sql statement executed was: SELECT `t`.`id` AS `t0_c0`, `t`.`qr_id` AS `t0_c1`, `t`.`given_name` AS `t0_c2`, `t`.`family_name` AS `t0_c3`, `t`.`madin_name` AS `t0_c4`, `t`.`dob` AS `t0_c5`, `t`.`dod` AS `t0_c6`, `t`.`country_of_birth` AS `t0_c7`, `t`.`country_of_death` AS `t0_c8`, `t`.`gender` AS `t0_c9`, `t`.`profile` AS `t0_c10`, `t`.`epitaph` AS `t0_c11`, `t`.`site_id` AS `t0_c12`, `t`.`title` AS `t0_c13` FROM `record` `t` WHERE (record_owner.user_id LIKE :ycp0) ORDER BY `t`.`given_name` LIMIT 25 

我该怎么做这个加入和过滤?

解决方法:

添加$criteria-> together = true;到搜索方法.

看一下这个解释:

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#together-detail

特别是,

When this property is not set, if the primary table is limited or
paginated, a sql statement will be executed for each HAS_MANY
relation. Otherwise, a single sql statement will be executed for all.

由于您没有设置此值,并且正在使用分页,因此将通过对到达结果的单独查询获取record_owners.当然,假设查询实际完成了.

通过设置$criteria-> together = true;您强制执行单个查询查询,这通过执行表连接来完成,这是您希望通过相关表中的一列过滤查询.

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

相关推荐