2017-11-08 9 views
0

나는 Yii를 처음 접하고 몇 가지 생각을하고 고민 중입니다.Yii2 ActiveRecord

나는이 기능 모델을 가지고 :이 방법과 같이 호출

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

null이 반환됩니다.

어떤 형태의 설명/정보를 주시면 감사하겠습니다.

답변

1

where을 사용하는 경우 추가하지 않고 쿼리의 where 부분을 설정합니다. 따라서 findNonGeo 함수에서 필수 부분을 빌드하고 있습니다. 그러나 CallerIdentity::findNonGeo()->where(['cidref' => $id])을 사용하면 이미 추가 된 부분을 제거 할 수 있습니다.

는 다음과 같이하십시오 :

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

andWhere를 사용하여 당신이 다른 부분하겠습니다 그냥 거기에 또 하나를 추가 할 수 있습니다.