PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 12, 2022

[FIXED] Yii2 ActiveRecord

 January 12, 2022     activerecord, yii     No comments   

Issue

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.


Solution

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.



Answered By - Jap Mul
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing