【问题标题】:Yii2 ActiveRecordYii2 活动记录
【发布时间】:2017-11-08 11:57:22
【问题描述】:

我是 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);

返回空值。

不胜感激任何形式的解释/信息。

【问题讨论】:

    标签: activerecord yii


    【解决方案1】:

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

    试试这样:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多