use alias in joinWith yii2

I’m trying to connect a relation table, and and get the max value

$query->joinWith('hotelPrices')
  ->andWhere([HotelPrice::tableName() . '.price' => $form->prices])
  ->max(HotelPrice::tableName() . '.price');

but i get this error

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: ‘hotel_price’

how to me in this case to use alias?

>Solution :

The error message indicates that you have used the same table alias (‘hotel_price’) in multiple places in your query. You can use a different alias for the ‘hotel_prices’ table in your join statement.

 $query->joinWith(['hotelPrices' => function($query) {
 $query->from(['hp' => HotelPrice::tableName()]);
 }])
 ->andWhere(['hp.price' => $form->prices])
 ->max('hp.price');

Leave a Reply