yiisoft / yiisoft/active-record

confused about viaTable()

Open
#8 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status:to be verified
Dominant language
PHP
Stars
119
Forks
38
Avg merge
1h 11m
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the viaTable() relationship shown in the issue and inspect the related Active Record query path. Reproduce the three queries with the provided Order, Item, and order_item tables, then determine whether the empty junction result and the item query using 0=1 are expected behavior or a bug.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.