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

PersistentCollection::clear() modifies uow for DEFERRED_EXPLICIT change tracking policy #7758

Closed
paxal opened this issue Jun 25, 2019 · 1 comment
Assignees
Labels
Milestone

Comments

@paxal
Copy link
Sponsor Contributor

paxal commented Jun 25, 2019

Bug Report

Q A
BC Break no
Version 2.6.3

Summary

Calling clear() on a Many-to-many association collection should not modify uow as long as persist() is not called on owner.
This is not the same behavior as calling removeElement() on each element, which sounds weird.

Current behavior

Calling clear() generates a DELETE FROM table WHERE owner_id = ? statement on a flush().

How to reproduce

Mapping :

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">
    <entity name="Foo"
            change-tracking-policy="DEFERRED_EXPLICIT">
        <id name="id" type="integer" column="id">
            <generator strategy="IDENTITY"/>
        </id>
        <many-to-many field="bars" target-entity="Bar">
            <cascade>
                <cascade-all/>
            </cascade>
            <join-table name="foo_bar">
                <join-columns>
                    <join-column name="foo_id" nullable="false"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="bar_id" nullable="false"/>
                </inverse-join-columns>
            </join-table>
        </many-to-many>
    </entity>
    <entity name="Bar"
            change-tracking-policy="DEFERRED_EXPLICIT">
        <id name="id" type="integer" column="id">
            <generator strategy="IDENTITY"/>
        </id>
    </entity>
</doctrine-mapping>

with PHP code :

class Bar { public $id; }
class Foo {
    public $id;
    public $bars;
    public function __construct()
    {
        $this->bars = new ArrayCollection();
    }
}

// Create new entity
$foo = new Foo();
// Attach Bar
$foo->bars->add(new Bar());
// Persist the whole thing
$em->persist($foo);
$em->flush();
// Retrieve assigned id
$fooId = $foo->id;
// Clear em
$em->clear();

// Fetch from DB
$foo = $em->find(Foo::class, $fooId);
// Clear bars item per item
foreach ($foo->bars as $bar) {
    $foo->bars->removeElement($bar);
}
// Does nothing as $foo is not persisted
$em->flush();

$foo->bars->clear();
// Removes all elements even though $foo is not persisted
$em->flush();

Expected behavior

No statement should be executed.

@lcobucci
Copy link
Member

lcobucci commented Oct 2, 2019

Handled by #7761

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

No branches or pull requests

2 participants