Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

confused about viaTable() #8

Open
xchl opened this issue Nov 2, 2018 · 2 comments
Open

confused about viaTable() #8

xchl opened this issue Nov 2, 2018 · 2 comments
Labels
type:enhancement Enhancement

Comments

@xchl
Copy link

xchl commented Nov 2, 2018

What steps will reproduce the problem?

for example I have three tables. order , item , order_item

Class Order extends ActiveRecord
{
    public function getItems()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->viaTable('order_item', ['order_id' => 'id']);
    }
}
Order::findOne(1)

The querys
1.select * from order where id=1;
2.select * from order_item where order_id = 1;(empty result)
3.select * from item where 0=1; (why have this? );
I looked the source code
image

What is the expected result?

What do you get instead?

Additional info

Q A
Yii version 2.0.5
PHP version 7.1.9
Operating system
@cebe
Copy link
Member

cebe commented Nov 2, 2018

3.select * from item where 0=1; (why have this? );

the condition in where could be much more complex than in your case, it is not easily possible for Yii to figure out whether a query result will be empty, so we build a query and let the database decide.

It might be possible to optimize this in some way though.

@samdark samdark transferred this issue from yiisoft/yii2 Nov 3, 2018
@samdark samdark added the type:enhancement Enhancement label Nov 3, 2018
@rob006
Copy link
Contributor

rob006 commented Nov 3, 2018

Can you check this with last version of Yii? In 2.0.11 support for emulateExecution was added, which should avoid such dummy queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

4 participants