Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading