FindOne with activequery and duplicate fields

I’ve been trying to set scopes on my model and that’s goin well until i use fields that are ambiguous throw a error because the mostly methods don’t implement alias for default (or i just don’t know that yet). Exist a way to set alias default for model or activequery?

My activequery class

`class ContratoQuery extends \yii\db\ActiveQuery

{

public function orgaoFilter()

{

if (!\Yii::$app->user->isSuperadmin)

return $this->innerJoin(‘fornecedor’, ‘fornecedor.ID = contrato.FK_FORNECEDOR’)

->andWhere([‘fornecedor.FK_ORGAO’ => \Yii::$app->user->getOrgao()]);

else

return $this;

}

public function all($db = null)

{

$this->orgaoFilter();


return parent::all($db);

}

public function one($db = null)

{

$this->orgaoFilter();


return parent::one($db);

}

}`

My model class without the relations

`class Contrato extends MainModel

{

public static function tableName()

{

return ‘contrato’;

}

public function rules()

{

return [

[[‘FK_FORNECEDOR’, ‘FK_LICITACAO’, ‘NUMERO’, ‘TIPO_CONTRATO’, ‘DATA’, ‘VALOR_TOTAL’], ‘required’],

[[‘FK_FORNECEDOR’, ‘FK_LICITACAO’], ‘integer’],

[[‘DATA’], ‘safe’],

[[‘DATA’], ‘date’],

[[‘VALOR_TOTAL’], ‘number’, ‘numberPattern’ => ‘/^$?((1-9)|0)(,\d{1,2})?$/’, ‘min’ => ‘0,01’],

[[‘NUMERO’], ‘string’, ‘max’ => 16],

[[‘TIPO_CONTRATO’], ‘string’, ‘max’ => 45],

[[‘FK_FORNECEDOR’], ‘exist’, ‘skipOnError’ => true, ‘targetClass’ => Fornecedor::className(), ‘targetAttribute’ => [‘FK_FORNECEDOR’ => ‘ID’]],

[[‘FK_LICITACAO’], ‘exist’, ‘skipOnError’ => true, ‘targetClass’ => Licitacao::className(), ‘targetAttribute’ => [‘FK_LICITACAO’ => ‘ID’]],

];

}

}`

So, when i try to use findOne method throws a http 500 with message :

*Exception (Integrity constraint violation) ‘yii\db\IntegrityException’ with message ‘SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘ID’ in where clause is ambiguous. The SQL being executed was: SELECT t. FROM contrato t INNER JOIN fornecedor ON fornecedor.ID = t.FK_FORNECEDOR WHERE (ID=‘1’) AND (fornecedor.FK_ORGAO=1)’ **

The solution that i thought is that implement a alias for findOne method but i can’t realize how i can do that or if this is the correct way to do that.

| Yii version | 2.0.5

| PHP version | 7.0

| Operating system | Linux Mint 18.2

Solved. I must set the first table name at all findOne queries.

Like Contrato::findOne($id) become Contrato::findOne([‘contrato.ID’ => $id]);