M
M
mitaichik2015-12-02 17:48:06
Yii
mitaichik, 2015-12-02 17:48:06

How to set a table alias in joinWith?

Hello!
In yii1, when requesting with with, the relay table was set by default with an alias - the name of the relay. Can this be done in yii2?
Simple example: there are tables
tbl_post
tbl_user
I write:

User::find()
            ->joinWith('posts')
            ->where(['username' => 'any', 'posts.active' => 1])
            ->all();

It will not work in the post.active condition - since the table is called tbl_post. Yii1 would write an alias, but yii2 doesn't.
You can, of course, write 'tbl_post.active' - but this is not very convenient - you have to write the name of the table in any such query ...
Tell me how to do it right?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Khomenko, 2015-12-02
@mitaichik

->joinWith( [ 
    'posts' => function ( ActiveQuery $query ) {
        $query->andWhere( ... );
    }
] )

65d2c-clip-81kb.png?nocache=1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question