Yii2 活动记录

Yii2 ActiveRecord

我是 Yii 的新手,正在努力了解一些事情。

我有这个功能的模型:

public static function findNonGeo()
{

    $query = new CallerIdentityQuery(get_called_class());
    $query->select([
        'cidref', 'caller_id', 'expiry', 'conf_call',
        'type', 'redirect', 'destination', 'status', 'start_date',
        'statusDesc' => new \yii\db\Expression("CASE status 
            WHEN 0 THEN 'Deactivated' 
            WHEN 1 THEN 'Active' 
            WHEN 2 THEN 'Inactive' 
            WHEN 3 THEN 'Unregistered'
            ELSE 'Permanently Deleted' END")])
        ->where(['status' => '1'])
        ->andWhere(['type' => 'N'])
        ->andWhere(['custref' => Yii::$app->user->identity->typedIdentity->getId()]);
    return $query;
}

在我的控制器中,我这样调用这个方法:

 $model = CallerIdentity::findNonGeo()->where(['cidref' => $id])->one();

但是当返回数据时,它就像忽略了

->andWhere(['type' => 'N'])

因为我可以查看非 'N' 类型的记录。 所以我必须在我的控制器中做,也许我做错了什么或者只是不理解但我不明白:

 $model = CallerIdentity::findNonGeo()->where(['cidref' => $id])->andWhere(['type'=>'N'])->one();

使用 findOne($id) 时的另一件事:

 $model = CallerIdentity::findOne($id);

返回空值。

感谢任何形式的 explanation/info。

使用 where 时,您设置的是查询的 where 部分,而不是添加到其中。因此,在 findNonGeo 函数中,您正在构建所需的 where 部分。但是使用 CallerIdentity::findNonGeo()->where(['cidref' => $id]) 你要删除已经添加的 where 部分。

这样试试:

CallerIdentity::findNonGeo()->andWhere(['cidref' => $id])->one();

通过使用 andWhere,您将保留其他部分并添加另一个部分。