E
E
EVOSandru62017-05-12 21:38:51
Yii
EVOSandru6, 2017-05-12 21:38:51

What is the correct way to do search filter and sort in Yii2 GridView with two fields from the same table?

Good afternoon,
I made a filter on the associated model, guided by these articles:
webdishes.ru/filter
https://nix-tips.ru/yii2-sortirovka-i-filtr-gridvi...
The fact is that my main model is - Orders refers to the Places model with 2 foreign keys - place_from_id , place_to_id .
The following code example handles filtering for placeFrom :

class OrdersSearch extends Orders
{

    public $placeFromName;
    public $placeToName;
...
public function rules()
    {
        return [
...
            [['placeFromName','placeToName'], 'safe']
   ....
  ];
}
....

public function search($params)
    {
        $query = Orders::find();

        $query->joinWith(['placeFrom']); // , 'placeTo

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $dataProvider->sort->attributes['placeFrom'] = [
            'asc' => ['t_places.name' => SORT_ASC],
            'desc' => ['t_places.name' => SORT_DESC],
        ];
        $dataProvider->sort->attributes['placeTo'] = [
            'asc' => ['t_places.name' => SORT_ASC],
            'desc' => ['t_places.name' => SORT_DESC],
        ];

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

       $query->andFilterWhere([
            'place_from_id' => $this->place_from_id,
            'place_to_id' => $this->place_to_id,
         ....
        ]);

        $query
            ->andFilterWhere(['like', 'LOWER(t_places.name)', strtolower($this->placeFromName)])
            ->andFilterWhere(['like', 'LOWER(t_places.name)', strtolower($this->placeToName)])
        ;
        return $dataProvider;
    }
...
}

But I need to search for 2 fields if I replace: with: I catch: Database Exception – yii\db\Exception SQLSTATE[42712]: Duplicate alias: 7 ERROR: table name "t_places" specified more than once The SQL being executed was: SELECT COUNT(*) FROM "m_orders" LEFT JOIN "t_places" ON "m_orders"."place_from_id" = "t_places"."id" LEFT JOIN "t_places" ON "m_orders"."place_to_id" = "t_places" ."id" WHERE (m_orders.exist != 2) AND ((("status_id" = 5) OR ("status_id" = 4)) OR ("driver_id"=9)) Error Info: Array ( [0] = > 42712 [1] => 7 [2] => ERROR: table name "t_places"specified more than once ↵ Caused by : PDOException
$query->joinWith(['placeFrom']); // , 'placeTo
$query->joinWith(['placeFrom' , 'placeTo]);

SQLSTATE[42712]: Duplicate alias: 7 ERROR: table name "t_places" specified more than once

I understand that it seems wrong to join the same table 2 times.
Tell me how to do it right so that the filter passes correctly on 2 fields that refer to the same table?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
P
padlyuck, 2017-05-12
@EVOSandru6

$query->joinWith(['placeFrom placeFrom' , 'placeTo placeTo']);// добавили алиасы
$query
            ->andFilterWhere(['like', 'LOWER(placeFrom.name)', strtolower($this->placeFromName)])
            ->andFilterWhere(['like', 'LOWER(placeTo.name)', strtolower($this->placeToName)])
        ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question