How to avoid ambiguous field while reusing query shorthand in Yii2?

Hi all, I’m a bit stuck in using Yii ActiveQuery shorthand when joining tables with a lot of column with similar names. How to avoid the ambiguous columns?




CREATE TABLE A (

    id INT,

    relationId INT,

    status INT

)




CREATE TABLE B (

    id INT,

    status INT

)



The class file is like below




class A 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 something like this




$model = A::find()

            ->joinWith([

                'b' => function(BQuery $query) {

                     $query->isNotActive();

                }

            ])

            ->isActive()

            ->one();



This will produce error




Column 'status' in where clause is ambiguous"



The only way I know is to manually add alias to $query->from and rewrite the $query->andWhere. But is there any easier way to reuse the query shorthand?

(PS: Also posted in StackOverflow http://stackoverflow.com/questions/30839188/how-to-avoid-ambiguous-field-while-reusing-query-shorthand-in-yii2)

not sure but try using this




 return $this->andWhere(['B.status' => 1]);