Problem with filter in GRIDVIEW

Problem with filter in GRIDVIEW

Hello community I am trying to filter and sort by the created_by username, updated_by.

The error that gives me is:

Duplicate alias: 7 ERROR: The table name "user" was specified more than once

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

Here I leave the model and the search model. I would greatly appreciate any help they can offer me.

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;


}

}

You should use an alias for the user table

I solved the problem thanks