Skip to content

confused about viaTable()  #8

Open

Description

@xchl

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

Activity

  1. cebe commented on Nov 2, 2018

    @cebe
    Member

    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.

  2. transferred this issue fromyiisoft/yii2on Nov 3, 2018
  3. rob006 commented on Nov 3, 2018

    @rob006

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions