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

viaTable relation does not unlink same records which it selects #4932

Closed
ghost opened this issue Sep 5, 2014 · 6 comments
Closed

viaTable relation does not unlink same records which it selects #4932

ghost opened this issue Sep 5, 2014 · 6 comments

Comments

@ghost
Copy link

ghost commented Sep 5, 2014

Hi guys, I have relation 1:n via pivot table declared as

return $this->hasMany(User::className(), ['id' => 'user_id'])
                    ->viaTable('edu_course_users', ['course_id' => 'id'], function (ActiveQuery $relation) {
                        $relation->andWhere('role = :role', [':role' => EduCourseUsers::ROLE_TEACHER]);
                    });

The select query which it makes is OK

SELECT * FROM `edu_course_users` WHERE (role = 'teacher') AND (`course_id`=1)

But the delete query, produced when I am executing

$this->unlinkAll('teachers', true)

is not OK

DELETE FROM `edu_course_users` WHERE `course_id`=1

It does not contain the additional where clause, which is present in the SELECT query.

@padlyuck
Copy link

Sorry for duplicates

@undefinedor
Copy link
Contributor

I write a test to verify this issue. but there seems to be no problem. The issue should close.

@undefinedor
Copy link
Contributor

// ActiveRecordTest.php
    public function testUnlinkAllOnConditionViaTable()
    {
        /** @var Order $orderClass */
        $orderClass = $this->getOrderClass();
        /** @var Item $itemClass */
        $itemClass = $this->getItemClass();

        // Ensure there are three items with category_id = 2 in the Items table
        $itemsCount = $itemClass::find()->where(['category_id' => 2])->count();
        $this->assertEquals(3, $itemsCount);

        $orderQuery = $orderClass::find()->with('limitedItems')->where(['id' => 2]);
        // Ensure that limitedItems relation returns only one item
        // (category_id = 2 and id in (4, 5))
        $category = $orderQuery->one();
        $this->assertCount(2, $category->limitedItems);

        // Unlink all items in the limitedItems relation
        $category->unlinkAll('limitedItems', true);

        // Call $orderQuery again to ensure that links are removed
        $this->assertCount(0, $orderQuery->one()->limitedItems);

        // Make sure that only links were removed, the items were not removed
        $this->assertEquals(3, $itemClass::find()->where(['category_id' => 2])->count());
    }

@undefinedor
Copy link
Contributor

The test code has covered this issue.

@samdark samdark added type:test and removed severity:normal status:to be verified Needs to be reproduced and validated. type:bug Bug labels Jun 10, 2020
@samdark samdark added this to the 2.0.36 milestone Jun 10, 2020
@samdark
Copy link
Member

samdark commented Jun 10, 2020

Interesting. Thanks for doing that.

  1. We need the test in our test set if it checks something we haven't checked before.
  2. How does it relate to unlink and link should also use the via on conditions of the relation #17174?

@samdark
Copy link
Member

samdark commented Jun 22, 2020

Seems it was fixed by #12213. Thanks, @undefinedor

@samdark samdark closed this as completed Jun 22, 2020
@samdark samdark removed this from the 2.0.36 milestone Jun 22, 2020
@samdark samdark removed the type:test label Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants