8000 PersistentCollection::clear() modifies uow for DEFERRED_EXPLICIT change tracking policy · Issue #7758 · doctrine/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
PersistentCollection::clear() modifies uow for DEFERRED_EXPLICIT change tracking policy #7758
Closed
@paxal

Description

@paxal

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0