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

Eager loading doesn't work #3929

Closed
alexk984 opened this issue Jun 17, 2014 · 3 comments
Closed

Eager loading doesn't work #3929

alexk984 opened this issue Jun 17, 2014 · 3 comments
Labels
status:to be verified Needs to be reproduced and validated.

Comments

@alexk984
Copy link

I have sql query with eager loading

$shows = UserShow::find()
    ->where(['user_id' => \Yii::$app->user->id])
    ->innerJoinWith('show')->asArray()->all();

But it is doing 2 queries:

SELECT `user__shows`.* FROM `user__shows` 
INNER JOIN `shows` ON `user__shows`.`show_id` = `shows`.`id` 
WHERE `user_id`='1'

And then

SELECT * FROM `shows` WHERE `id` IN ('1', '2', '3')

Relation is

public function getShow()
{
    return $this->hasOne(Show::className(), ['id' => 'show_id']);
}

Is it a bug?

@samdark samdark added this to the 2.0 RC milestone Jun 17, 2014
@qiangxue
Copy link
Member

This is by design (eager loading in 2.0 is different from 1.1).

@ljames604
Copy link

This is a very unfortunate liability of using Yii2. Making 80% unnecessary database calls it totally unacceptable. This would only suffice for the simplest of database models. Any application that requires many table joins would severely suffer from this design flaw. Any application who uses a remote or external database connection, which is very common on apps hosted in the cloud, would suffer greatly as well.

This would make an EAV database model totally out of the question. I have yet to encounter another framework that does not allow this functionality. The performance boost greatly outweighs the complexity.

@klimov-paul
Copy link
Member

You may use following extension to get rid of redundant queries:
https://github.com/yii2tech/ar-eagerjoin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated.
Projects
None yet
Development

No branches or pull requests

6 participants