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

Thursday, March 3, 2022

[FIXED] Yii 2 can rewrite WHERE condition from model?

 March 03, 2022     sql, yii, yii2, yii2-basic-app     No comments   

Issue

I have some where condition in my model . Its check is field active or no.

Now I need to write a join relation. But I need to remove where condition. Is it possible?

My model.

  ...
  public static function find() {
     return (new AssetgroupsQuery(get_called_class()))->active();
  }

My relation

public function getAssetgroup(): \app\models\AssetgroupsQuery {
    return $this->hasOne(Assetgroups::class, ['asg_id' => 'ass_group'])->andOnCondition(['asg_active' => '1'])
        ->viaTable('assets', ['ass_id' => 'log_ass_id',]);
}

I need to got all active assets and join, if asset is empty I need to got null fields, but model where condition added to my current sql query and remove all fields which assets are null. I try to add some where Condition to remove old where, but it don't work.

Can you help me?


Solution

You can reset existing conditions by using where(null).

On relation level:

public function getAssetgroup(): \app\models\AssetgroupsQuery {
    return $this->hasOne(Assetgroups::class, ['asg_id' => 'ass_group'])
        ->andOnCondition(['asg_active' => '1'])
        ->where(null)
        ->viaTable('assets', ['ass_id' => 'log_ass_id',]);
}

Or directly on join:

$query = MyModel::find()
    ->joinWith([
        'assetgroup' => function (ActiveQuery $query) {
            $query->where(null);
        },
    ])


Answered By - rob006
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

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