Yii使用CDbCriteria和CActiveDataProvider与自定义搜索联接

时间:2013-11-15 13:03:37

标签: yii relational-database cgridview cactivedataprovider

您好我正在使用Yii创建应用程序

我修改了模型中的search(),我遇到了一个问题。

用户可以以管理员,经理,客户

的身份登录

当用户被记录为经理时,他只能从他们都属于的商店查看客户。到目前为止,我已经成功实现了这一目标。

现在问题是当我试图阻止管理员在CGridView中查看来自同一商店的其他经理(因此编辑彼此的帐户)时。

关系

/**
 * @return array relational rules.
 */
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(
        'authitems' => array(self::MANY_MANY, 'Authassignment', 'authassignment(userid, itemname)'),
        'additionalContacts' => array(self::HAS_MANY, 'AdditionalContact', 'Client_Id'),
        'store' => array(self::BELONGS_TO, 'Store', 'Store_Id'),
        'user' => array(self::BELONGS_TO, 'User', 'User_Id'),
        'genericPoints' => array(self::HAS_MANY, 'GenericPoint', 'Client_Id'),
    );
}

自定义模型搜索

public function searchCustom()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;
    $criteria->addCondition('t.id !='.$this->id);
    $criteria->compare('t.Created',$this->Created,true);
    $criteria->compare('t.Updated',$this->Updated,true);
    $criteria->compare('t.Discount',$this->Discount,true);
    $criteria->compare('t.Discount_Type',$this->Discount_Type,true);
    $criteria->addCondition('t.Store_Id ='.$this->Store_Id);

    //$criteria->addCondition('t.id !='.$this->id);

    //GET FIELDS FROM USER IN SEARCH

    $criteria->with=array('user');
    $criteria->compare('user.id',$this->User_Id,true);
    $criteria->compare('user.First_Name',$this->First_Name,true);
    $criteria->compare('user.Last_Name',$this->Last_Name,true);
    $criteria->compare('user.Username',$this->Username,true);
    $criteria->compare('user.Email',$this->Email,true);
    $criteria->addCondition('user.Status = 1');

    $criteria->with=array('authitems');
    $criteria->compare('authitems.userid',$this->id,false);
    $criteria->compare('authitems.itemname','client',false);

    $criteria->together = true;

    $criteria->order = 't.Created DESC';
    return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
    ));
}

这就是我得到的错误

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user.Status' in 'where clause'. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`id`) FROM `client` `t` LEFT OUTER JOIN `authassignment` `authitems_authitems` ON (`t`.`id`=`authitems_authitems`.`userid`) LEFT OUTER JOIN `authassignment` `authitems` ON (`authitems`.`itemname`=`authitems_authitems`.`itemname`) WHERE (((((t.id !=2) AND (t.Store_Id =1)) AND (user.Status = 1)) AND (authitems.userid=:ycp0)) AND (authitems.itemname=:ycp1))

我知道这与gii为我设定的关系有关,但我无法确定它。

也许authitems的'MANY_MANY关系会阻止用户关系加载?

1 个答案:

答案 0 :(得分:0)

不应为$criteria->with进行另一项任务(将覆盖前一项),您应该尝试:

$criteria->with=array('user', 'authitems');