将MySQL查询转换为Yii CDbCriteria

时间:2015-01-07 01:30:42

标签: php mysql yii

我有两个名为

的表
  1. new_tbl_master_siteid - >型号:SiteID
  2. app_tbl_ict_dapot_genset - >型号:发电机组
  3. 我有一个像这样的mysql查询

    SELECT A.siteid, B.type, B.status_operations, B.status
    FROM new_tbl_master_siteid as A
    LEFT JOIN app_tbl_ict_dapot_genset as B
    ON A.siteid = B.siteid
    

    我想离开的地方加入 siteid

    我正在考虑修改genset模型上的CdbCriteria,我该如何做到这一点,因此genset gridview的数据提供者也可以使用SiteID模型中的列?

    发电机组模型的标准

    $criteria=new CDbCriteria;
    
            $criteria->compare('id',$this->id);
            $criteria->compare('siteid',$this->siteid,true);
            $criteria->compare('merk',$this->merk,true);
            $criteria->compare('type',$this->type,true);
            $criteria->compare('capacity',$this->capacity,true);
            $criteria->compare('kapasitas_tangki',$this->kapasitas_tangki,true);
            $criteria->compare('status',$this->status,true);
            $criteria->compare('status_operations',$this->status_operations,true);
    
            return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
    

    ==================

    以下是

    的关系
    class genset 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(
                'siteids'=>array(self::BELONGS_TO,'SiteID','siteid'),
            );
        }
    ....
    
    public function search()
        {
            // @todo Please modify the following code to remove attributes that should not be searched.
    
            $criteria=new CDbCriteria;
    
            $criteria->compare('id',$this->id);
            $criteria->compare('siteid',$this->siteid,true);
            $criteria->compare('merk',$this->merk,true);
            $criteria->compare('type',$this->type,true);
            $criteria->compare('capacity',$this->capacity,true);
            $criteria->compare('kapasitas_tangki',$this->kapasitas_tangki,true);
            $criteria->compare('status',$this->status,true);
            $criteria->compare('status_operations',$this->status_operations,true);
            $criteria->with = array('siteids.rtp');
            $criteria->compare('siteids.rtp',$this->rtp,true);
    
            return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
            ));
        }
    

    和siteid类

    class SiteID 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(
                'gensets'=>array(self::HAS_MANY, 'genset', 'siteid'),   
            );
        }
    

    但我收到了错误

    Relation "rtp" is not defined in active record class "SiteID".
    

    rtp是siteid模型中的一列

2 个答案:

答案 0 :(得分:1)

Yii这样做的方法是通过关系,例如你应该将连接表的关系添加到你的模型中(如果它还没有)然后你可以直接访问相关模型的属性,例如,如果SiteID对象是$ model和关系的名称是'genset',你可以得到genset的状态:$ model-> genset-> status

答案 1 :(得分:0)

你要做的第一件事是在模型之间建立关系,可以这样做:

public function relations()
{
        return array(
            'genset'=>array(self::HAS_MANY, 'genset', 'siteid'),
        );
}

在模型类中。在两个模型中定义关系后,修改您的条件以使用->with

加入结果
$criteria->with = array('genset');

你准备好了。但请确保在两个模型中定义关系。