M
M
Mikhail Smirnov2016-11-11 16:17:28
Yii
Mikhail Smirnov, 2016-11-11 16:17:28

How to build queries in constructor with JOIN in Yii2?

Good afternoon, I
need to make the following request:

Comments::find()
                ->select('с.text, с.date_create, с.firstname, с.lastname')
                ->innerJoin('{{%users}} as u', 'с.user_id  = u.id')
                ->where(['с.status' => Comments::STATUS_PUBLIC])

How can I set the short name "c" for the comments table?
And another question:
How can I access attached data?
this query is used in ActiveDataProvider

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlikDex, 2016-11-11
@fortoster83

Comments::find()
    ->select('с.text, с.date_create, с.firstname, с.lastname')
    ->from([Comments::tableName() . ' с'])
    ->innerJoin('{{%users}} as u', 'с.user_id  = u.id')
    ->where(['с.status' => Comments::STATUS_PUBLIC])

M
Maxim Fedorov, 2016-11-11
@qonand

How can I set the short name "c" for the comments table?

Comments::find()->form(['c' => 'comments'])
but in general it is better to use standard data binding tools

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question