Problema con filtro en el GRIDVIEW

Hola comunidad estoy tratando de filtrar y ordenar por el username de created_by,updated_by.

El error que me da es:

Duplicate alias: 7 ERROR: el nombre de tabla «user» fue especificado más de una vez

The SQL being executed was: SELECT COUNT(*) FROM "anno" LEFT JOIN "user" ON "anno"."created_by" = "user"."id" LEFT JOIN "user" ON "anno"."updated_by" = "user"."id"

SQLSTATE[42712]: Duplicate alias: 7

Aca les dejo el model y el search model.Agradeceria mucho cualquier ayuda que me puedan ofrecer.

MODEL:

<?php

namespace app\models;

use Yii;

use yii\behaviors\BlameableBehavior;

use yii\db\Expression;

use yii\db\ActiveRecord;

/**

  • This is the model class for table "anno".

  • @property integer $id

  • @property string $created_at

  • @property string $updated_at

  • @property integer $created_by

  • @property integer $updated_by

  • @property User $createdBy

  • @property User $updatedBy

*/

class Anno extends \yii\db\ActiveRecord

{

/**


 * @inheritdoc


 */


public static function tableName()


{


    return 'anno';


}





/**


 * @inheritdoc


 */


public function rules()


{


    return [


        [['id', 'created_at', 'created_by'], 'required'],


        [['id', 'created_by', 'updated_by'], 'integer'],


        [['created_at', 'updated_at'], 'safe'],


        [['created_by'], 'exist', 'skipOnError' =&gt; true, 'targetClass' =&gt; User::className(), 'targetAttribute' =&gt; ['created_by' =&gt; 'id']],


        [['updated_by'], 'exist', 'skipOnError' =&gt; true, 'targetClass' =&gt; User::className(), 'targetAttribute' =&gt; ['updated_by' =&gt; 'id']],


    ];


}





 /**


 * @inheritdoc


 */


public function attributeLabels()


{


    return [


        'id' =&gt; 'ID',


        'created_at' =&gt; 'Created At',


        'updated_at' =&gt; 'Updated At',


        'created_by' =&gt; 'Created By',


        'updated_by' =&gt; 'Updated By',


    ];


}





/**


 * @return &#092;yii&#092;db&#092;ActiveQuery


 */


public function getCreatedBy()


{


    return &#036;this-&gt;hasOne(User::className(), ['id' =&gt; 'created_by']);


}





/**


 * @return &#092;yii&#092;db&#092;ActiveQuery


 */


public function getUpdatedBy()


{


    return &#036;this-&gt;hasOne(User::className(), ['id' =&gt; 'updated_by']);


}

}

SEARCH MODEL:

<?php

namespace app\models\Search;

use Yii;

use yii\base\Model;

use yii\data\ActiveDataProvider;

use app\models\Anno;

/**

  • AnnoSearch represents the model behind the search form about app&#092;models&#092;Anno.

*/

class AnnoSearch extends Anno

{

/**


 * @inheritdoc


 */


public function rules()


{


    return [


        [['id'], 'integer'],


        [['created_at', 'updated_at','created_by', 'updated_by'], 'safe'],


    ];


}





/**


 * @inheritdoc


 */


public function scenarios()


{


    // bypass scenarios() implementation in the parent class


    return Model::scenarios();


}





/**


 * Creates data provider instance with search query applied


 *


 * @param array &#036;params


 *


 * @return ActiveDataProvider


 */


public function search(&#036;params)


{


    &#036;query = Anno::find();


    &#036;query-&gt;joinWith('createdBy')


    &#036;query-&gt;joinWith('updatedBy');





    &#036;dataProvider = new ActiveDataProvider([


        'query' =&gt; &#036;query,


        'pagination'    =&gt; [


            'pageSize'  =&gt; 10,


        ],


        'sort'  =&gt; [


            'defaultOrder'  =&gt; [


                'anno'       =&gt; SORT_DESC,





            ],


            'attributes'    =&gt; [


                'anno',





                'created_by'   =&gt; [


                    'asc'   =&gt; ['user.username' =&gt; SORT_ASC],


                    'desc'  =&gt; ['user.username' =&gt; SORT_DESC],


                ],


                'updated_by'   =&gt; [


                    'asc'   =&gt; ['user.username' =&gt; SORT_ASC],


                    'desc'  =&gt; ['user.username' =&gt; SORT_DESC],


                ],


                'created_at',


                'updated_at'


            ]


        ]


    ]);





    &#036;this-&gt;load(&#036;params);





   





    if (&#33;&#036;this-&gt;validate()) {


        // uncomment the following line if you do not want to return any records when validation fails


        // &#036;query-&gt;where('0=1');


        return &#036;dataProvider;


    }





    &#036;query-&gt;andFilterWhere([


        'id' =&gt; &#036;this-&gt;id,


        'created_at' =&gt; &#036;this-&gt;created_at,


        'updated_at' =&gt; &#036;this-&gt;updated_at


        


    ]);





    &#036;query-&gt;andFilterWhere(['like','user.username', &#036;this-&gt;created_by])


          -&gt;andFilterWhere(['like','user.username', &#036;this-&gt;updated_by]);





    return &#036;dataProvider;


}

}

Estás haciendo dos LEFT JOIN a la misma tabla. Solo es necesario uno para asociar dos tablas, después, cada registro vendrá combinado y podrás acceder a la información.

Efectivamente, al usar las dos relaciones para la misma tabla la está llamando dos veces, por lo tanto da el error Duplicate alias, para solucionarlo puede llamarlo en un solo join




$query->joinWith(['createdBy' => function ($q) {

   $q->andFilterWhere(['=', 'user.username', $this->createdBy]);

   $q->andFilterWhere(['=', 'user.username', $this->updatedBy]);

}]);



Luego debe eliminarlo del primer filtro:




$query->andFilterWhere([

   'id' => $this->id,

   // 'created_by' => $this->created_by,

   'created_at' => $this->created_at,

   //'updated_by' => $this->updated_by,

   'updated_at' => $this->updated_at,

]);



Y agregar el like en el segundo filtro:




->andFilterWhere(['like', 'user.name', $this->created_by])

->andFilterWhere(['like', 'user.name', $this->updated_by])



OK,Gracias .Problema solucionado.