php - How to avoid ambiguous field while reusing query shorthand in Yii2? -
i have table below.
create table ( id int, relationid int, status int ) create table b ( id int, status int ) the class file below
class extends \yii\db\activerecord { public function getb() { return $this->hasone(b::class, ['id' => 'relationid']); } public function find() { return new aquery(__class__); } } class aquery extends \yii\db\query { public function isactive() { return $this->andwhere(['status' => 1]); } public function isnotactive() { return $this->andwhere(['status' => 0]); } } class b extends \yii\db\activerecord { public function find() { return new bquery(__class__); } } class bquery extends \yii\db\query { public function isactive() { return $this->andwhere(['status' => 1]); } public function isnotactive() { return $this->andwhere(['status' => 0]); } } i'm doing this
$model = a::find() ->joinwith([ 'b' => function(bquery $query) { $query->isnotactive(); } ]) ->isactive() ->one(); this produce error
column 'status' in clause ambiguous" the way know manually add alias $query->from , rewrite $query->andwhere. there easier way reuse query shorthand?
use activerecord::tablename() instead of aliasing (which doesn't seem active record feature in yii2). tablename() can accessed through modelclass property of \yii\db\activequery.
public function isactive() { $modelclass = $this->modelclass; return $this->andwhere([$modelclass::tablename().'.status' => 1]); }
Comments
Post a Comment