1

您好我正在使用 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 关系会阻止加载用户关系?

4

1 回答 1

0

而不是进行另一个分配$criteria->with(这将覆盖前一个),您应该简单地尝试:

$criteria->with=array('user', 'authitems');
于 2013-11-15T16:26:49.150 回答