S
S
Sergey Beloventsev2016-05-16 19:11:19
Yii
Sergey Beloventsev, 2016-05-16 19:11:19

Why doesn't leftJoin work?

This is how I write

Watch::find()->select(['fl_serial.*,COUNT(*) AS total_count']) ->leftJoin(['fl_serial','`fl_serial`.`id`=`fl_watch`.`id_serial`'])
->where(['fl_watch.active'=>1])->groupBy('fl_watch.id_serial')->orderBy(['total_count'=>SORT_DESC])->limit(10)->all();

get
Syntax error or access violation: 1064 You have an error in your SQL syntaxYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `fl_watch`.`active`=1 GROUP BY `fl_watch`.`id_serial` ORDER BY `total_coun' at line 1
The SQL being executed was: SELECT fl_serial.*,COUNT(*) AS total_count FROM `fl_watch` LEFT JOIN `fl_serial` WHERE `fl_watch`.`active`=1 GROUP BY `fl_watch`.`id_serial` ORDER BY `total_count` DESC LIMIT 10

I don’t understand why it’s not fully processed ->leftJoin(['fl_serial','fl_serial.id=fl_watch.id_serial']) tell me where I went wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-05-16
@Sergalas

If you use a framework - use its methods and principles. Create a connection in the model and do a join through it.
www.yiiframework.com/doc-2.0/guide-db-active-recor... namely the "Joining with Relations" section

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question