2

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?

Lola_Padilya
  • 107
  • 1
  • 7

1 Answers1

2

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);
        },
    ])
rob006
  • 21,383
  • 5
  • 53
  • 74
  • I use -> where([]) , but I'm not sure that it's right. Using null is better way? What's the better way in model or in controller? – Lola_Padilya Jul 28 '18 at 19:12
  • Empty array may also work, but `null` explicitly means "no where conditions", so I suggest using it as in my example. – rob006 Jul 28 '18 at 19:59