DDC-1504: Cascade remove in OneToMany relation doesn't work #1886

Closed
opened 2026-01-22 13:29:43 +01:00 by admin · 8 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 22, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user naelyth:

I have two entities :

class Structure
{
    // Id and other variables

    /****
     * @var ArrayCollection $employees liste des employés d'une structure
     *
     * @ORM\OneToMany(targetEntity="Uriae\EmployeeBundle\Entity\Employee", mappedBy="structure", cascade={"all"})
     */
    private $employees;
}

class Employee
{
    // Id and other variables

    /****
     * @var integer $structure
     *
     * @ORM\ManyToOne(targetEntity="Uriae\StructureBundle\Entity\Structure",inversedBy="employees")
     */
    private $structure;
}

The problem is when I used tests and I wan't remove a Structure the Employees aren't removed.

// Doesn't work
$em->remove($structure);
$em->flush();

// Bad solution but it works
$em->remove($structure);
$em->flush();
foreach ($employee as $employees)
{
    $em->remove($employee);
}
$e->flush();
Originally created by @doctrinebot on GitHub (Nov 22, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user naelyth: I have two entities : ``` class Structure { // Id and other variables /**** * @var ArrayCollection $employees liste des employés d'une structure * * @ORM\OneToMany(targetEntity="Uriae\EmployeeBundle\Entity\Employee", mappedBy="structure", cascade={"all"}) */ private $employees; } class Employee { // Id and other variables /**** * @var integer $structure * * @ORM\ManyToOne(targetEntity="Uriae\StructureBundle\Entity\Structure",inversedBy="employees") */ private $structure; } ``` The problem is when I used tests and I wan't remove a Structure the Employees aren't removed. ``` // Doesn't work $em->remove($structure); $em->flush(); // Bad solution but it works $em->remove($structure); $em->flush(); foreach ($employee as $employees) { $em->remove($employee); } $e->flush(); ```
admin added the Bug label 2026-01-22 13:29:43 +01:00
admin closed this issue 2026-01-22 13:29:44 +01:00
Author
Owner

@doctrinebot commented on GitHub (Nov 30, 2011):

Comment created by stefi023:

The same problem, described here
http://stackoverflow.com/questions/7766999/doctrine-2-0-vs-2-1-cascade-remove-onetomany

@doctrinebot commented on GitHub (Nov 30, 2011): Comment created by stefi023: The same problem, described here http://stackoverflow.com/questions/7766999/doctrine-2-0-vs-2-1-cascade-remove-onetomany
Author
Owner

@doctrinebot commented on GitHub (Nov 30, 2011):

Comment created by naelyth:

I know, it's me who answered. But the solution i've found is "ugly".

@doctrinebot commented on GitHub (Nov 30, 2011): Comment created by naelyth: I know, it's me who answered. But the solution i've found is "ugly".
Author
Owner

@doctrinebot commented on GitHub (Dec 15, 2011):

Comment created by @beberlei:

Fixed formatting

@doctrinebot commented on GitHub (Dec 15, 2011): Comment created by @beberlei: Fixed formatting
Author
Owner

@doctrinebot commented on GitHub (Dec 27, 2011):

Comment created by entea:

If you add orphanRemoval="true" it works.
But I think doctrine should somehow let the user know about this problem (throw exception, etc.)
Maybe it worths making orphanRemoval=true when cascading=remove?

@doctrinebot commented on GitHub (Dec 27, 2011): Comment created by entea: If you add **orphanRemoval="true"** it works. But I think doctrine should somehow let the user know about this problem (throw exception, etc.) Maybe it worths making **orphanRemoval=true** when cascading=remove?
Author
Owner

@doctrinebot commented on GitHub (Dec 28, 2011):

Comment created by @beberlei:

  1. orphanRemoval is not the right solution, it has different semantics. If you want this semantics the solution is ok.
  2. Has anyone tried updating to anything above 2.1.2?
  3. I cannot reproduce with a test-case against the current master or 2.1.5

I need a better testcase. Attached is the one i am working with.

@doctrinebot commented on GitHub (Dec 28, 2011): Comment created by @beberlei: 1. orphanRemoval is not the right solution, it has different semantics. If you want this semantics the solution is ok. 2. Has anyone tried updating to anything above 2.1.2? 3. I cannot reproduce with a test-case against the current master or 2.1.5 I need a better testcase. Attached is the one i am working with.
Author
Owner

@doctrinebot commented on GitHub (Mar 14, 2012):

Comment created by @beberlei:

No feedback given.

@doctrinebot commented on GitHub (Mar 14, 2012): Comment created by @beberlei: No feedback given.
Author
Owner

@doctrinebot commented on GitHub (Mar 14, 2012):

Issue was closed with resolution "Incomplete"

@doctrinebot commented on GitHub (Mar 14, 2012): Issue was closed with resolution "Incomplete"
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2015):

Imported 1 attachments from Jira into https://gist.github.com/6a106ee22e49bdee4fb3

@doctrinebot commented on GitHub (Dec 13, 2015): Imported 1 attachments from Jira into https://gist.github.com/6a106ee22e49bdee4fb3 - [11136_DDC1504GitlessTest.php](https://gist.github.com/6a106ee22e49bdee4fb3#file-11136_DDC1504GitlessTest-php)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1886