Kyle
Kyle

Reputation: 735

Yii2 ActiveRecord

I'm new to Yii and struggling to get my head around a few things.

I have model with this function:

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

And in my controller I call this method like so:

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

but when the data is returned its like its just ignoring the

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

because I can view records that aren't of type 'N'. so I'm having to do in my controller, maybe I'm doing something wrong or just not understanding it but I don't get it:

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

another thing when using findOne($id):

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

null is returned.

Would appreciate any form of explanation/info.

Upvotes: 0

Views: 32

Answers (1)

Jap Mul
Jap Mul

Reputation: 18729

When using where you're setting the where part of the query, not adding to it. So in the findNonGeo function you're building the required where parts. But with CallerIdentity::findNonGeo()->where(['cidref' => $id]) you're removing the already added where parts.

Try like this:

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

by using andWhere you'll keep the other parts and just add another one to it.

Upvotes: 1

Related Questions