DDC-406: A detached entity can not be removed, on @OneToOne cascade={"remove"} #504

Closed
opened 2026-01-22 12:40:41 +01:00 by admin · 11 comments
Owner

Originally created by @doctrinebot on GitHub (Mar 11, 2010).

Jira issue originally created by user else:

Dear developers,

I have class defined like this:


/****
 * @Entity
 * @HasLifecycleCallbacks
 */
class Shop*Data_Entity_Customer extends Shop_Data_Entity*Guest {
    /****
     * @OneToOne(targetEntity="Shop*Data_Entity*Contact", cascade={"remove"})
     * @JoinColumn(name="contact", referencedColumnName="id")
     */
    protected $contact;

}

when i run this:

$q = $em->createQuery("select c from Shop*Data_Entity*Customer c");
$q->setMaxResults(1);
$customers = $q->getResult();
$em->remove($customers[0]);
$em->flush();

I get "A detached entity can not be removed".

I don't understand it very well. I need to remove associated entity but it's not done and exception occurs.

I tried it with (like OneToMany)

/****
     * @ManyToMany(targetEntity="Shop*Data_Entity*Contact", cascade={"persist","remove"} )
     * @JoinTable(name="Shop*Data_Entity_Customer_*homes",
     *      joinColumns={@JoinColumn(name="customer_id", referencedColumnName="id", onDelete="cascade" )},
     *      inverseJoinColumns={@JoinColumn(name="contact_id", referencedColumnName="id", onDelete="cascade" )}
     *      )
     */
public $homes;

and

$q = $em->createQuery("select c from Shop*Data_Entity*Customer c");
$q->setMaxResults(1);
$customers = $q->getResult();
$em->remove($customers[0]->homes[0]);
$em->flush();

worked without problem like this.

orphanRemoval set it's not working so.

Thank for solve and perfect ORM!!! ;-)

Andy

Originally created by @doctrinebot on GitHub (Mar 11, 2010). Jira issue originally created by user else: Dear developers, I have class defined like this: ``` /**** * @Entity * @HasLifecycleCallbacks */ class Shop*Data_Entity_Customer extends Shop_Data_Entity*Guest { /**** * @OneToOne(targetEntity="Shop*Data_Entity*Contact", cascade={"remove"}) * @JoinColumn(name="contact", referencedColumnName="id") */ protected $contact; } ``` when i run this: ``` $q = $em->createQuery("select c from Shop*Data_Entity*Customer c"); $q->setMaxResults(1); $customers = $q->getResult(); $em->remove($customers[0]); $em->flush(); ``` I get "A detached entity can not be removed". I don't understand it very well. I need to remove associated entity but it's not done and exception occurs. I tried it with (like OneToMany) ``` /**** * @ManyToMany(targetEntity="Shop*Data_Entity*Contact", cascade={"persist","remove"} ) * @JoinTable(name="Shop*Data_Entity_Customer_*homes", * joinColumns={@JoinColumn(name="customer_id", referencedColumnName="id", onDelete="cascade" )}, * inverseJoinColumns={@JoinColumn(name="contact_id", referencedColumnName="id", onDelete="cascade" )} * ) */ public $homes; ``` and ``` $q = $em->createQuery("select c from Shop*Data_Entity*Customer c"); $q->setMaxResults(1); $customers = $q->getResult(); $em->remove($customers[0]->homes[0]); $em->flush(); ``` worked without problem like this. orphanRemoval set it's not working so. Thank for solve and perfect ORM!!! ;-) Andy
admin added the Bug label 2026-01-22 12:40:41 +01:00
admin closed this issue 2026-01-22 12:40:41 +01:00
Author
Owner

@doctrinebot commented on GitHub (Mar 12, 2010):

Comment created by else:

Dear Roman,
When are you going to release 2.0-BETA1 [ 10030 ]? I ask you because i need this patch now to continue my developing.
Thank you very much.

Andy

@doctrinebot commented on GitHub (Mar 12, 2010): Comment created by else: Dear Roman, When are you going to release 2.0-BETA1 [ 10030 ]? I ask you because i need this patch now to continue my developing. Thank you very much. Andy
Author
Owner

@doctrinebot commented on GitHub (Mar 15, 2010):

Comment created by romanb:

Hi,

I created a test case (attached to this issue) that works well. Can you please modify that test so that it reflects your issue or tell me how to modify it so I can reproduce your problem? Thanks.

If you have the Doctrine tests checked out you can just copy this file into the tests/Doctrine/Tests/ORM/Functional/Ticket folder and run it with: "phpunit Doctrine/Tests/ORM/Functional/Ticket/DDC406Test".

@doctrinebot commented on GitHub (Mar 15, 2010): Comment created by romanb: Hi, I created a test case (attached to this issue) that works well. Can you please modify that test so that it reflects your issue or tell me how to modify it so I can reproduce your problem? Thanks. If you have the Doctrine tests checked out you can just copy this file into the tests/Doctrine/Tests/ORM/Functional/Ticket folder and run it with: "phpunit Doctrine/Tests/ORM/Functional/Ticket/DDC406Test".
Author
Owner

@doctrinebot commented on GitHub (Mar 15, 2010):

Comment created by else:

Dear Roman,
Check this issue at first: http://www.doctrine-project.org/jira/browse/DDC-422 Maybe my extending cause also this issue. If you can't reproduce it with my extending write me and i'll try your test.

Thank you.

Andy

@doctrinebot commented on GitHub (Mar 15, 2010): Comment created by else: Dear Roman, Check this issue at first: http://www.doctrine-project.org/jira/browse/[DDC-422](http://www.doctrine-project.org/jira/browse/DDC-422) Maybe my extending cause also this issue. If you can't reproduce it with my extending write me and i'll try your test. Thank you. Andy
Author
Owner

@doctrinebot commented on GitHub (Mar 16, 2010):

Comment created by else:

Dear Roman,
I can't understand how you could run that test.

First there wasn't set generatedValue strategy so i added it. It throw very stupid exception. Maybe it will be better to correct this error to understand what's wrong easier.

PHP Fatal error:  Undefined class constant 'Doctrine\ORM\Mapping\ClassMetadata::GENERATOR*TYPE*' in /home/else/download/doctrine/2.0.0-ALPHA4/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php on line 212

Then there wasn't set Column type so added either.

Then when i run test with "phpunit Doctrine/Tests/ORM/Functional/Ticket/DDC406Test.php" i got same error i reported.

I'm attaching edited test.

Andy

@doctrinebot commented on GitHub (Mar 16, 2010): Comment created by else: Dear Roman, I can't understand how you could run that test. First there wasn't set generatedValue strategy so i added it. It throw very stupid exception. Maybe it will be better to correct this error to understand what's wrong easier. ``` PHP Fatal error: Undefined class constant 'Doctrine\ORM\Mapping\ClassMetadata::GENERATOR*TYPE*' in /home/else/download/doctrine/2.0.0-ALPHA4/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php on line 212 ``` Then there wasn't set Column type so added either. Then when i run test with "phpunit Doctrine/Tests/ORM/Functional/Ticket/DDC406Test.php" i got same error i reported. I'm attaching edited test. Andy
Author
Owner

@doctrinebot commented on GitHub (Mar 16, 2010):

Comment created by romanb:

@GeneratedValue is the same as @GeneratedValue(strategy="AUTO"), AUTO is the default strategy, at least in trunk, and to run the test, you should check out trunk.

@Column is the same as @Column(type="string"), string is the default type.

@doctrinebot commented on GitHub (Mar 16, 2010): Comment created by romanb: @GeneratedValue is the same as @GeneratedValue(strategy="AUTO"), AUTO is the default strategy, at least in trunk, and to run the test, you should check out trunk. @Column is the same as @Column(type="string"), string is the default type.
Author
Owner

@doctrinebot commented on GitHub (Mar 16, 2010):

Comment created by romanb:

It makes no sense to test against ALPHA4, please use trunk.

@doctrinebot commented on GitHub (Mar 16, 2010): Comment created by romanb: It makes no sense to test against ALPHA4, please use trunk.
Author
Owner

@doctrinebot commented on GitHub (Mar 16, 2010):

Comment created by romanb:

Your new test still works fine for me, again using trunk. So your problem might be already fixed.

@doctrinebot commented on GitHub (Mar 16, 2010): Comment created by romanb: Your new test still works fine for me, again using **trunk**. So your problem might be already fixed.
Author
Owner

@doctrinebot commented on GitHub (Mar 16, 2010):

Comment created by else:

Dear Roman,

I checkouted trunk version and it solves this problem. I'm sorry that i consumed your time with that. I'm not used to solve problems like this. Hope i can help better now to make doctrine more stable.

Andy

@doctrinebot commented on GitHub (Mar 16, 2010): Comment created by else: Dear Roman, I checkouted trunk version and it solves this problem. I'm sorry that i consumed your time with that. I'm not used to solve problems like this. Hope i can help better now to make doctrine more stable. Andy
Author
Owner

@doctrinebot commented on GitHub (Mar 16, 2010):

Comment created by romanb:

OK, at least now you know already how to deal with test cases :)

@doctrinebot commented on GitHub (Mar 16, 2010): Comment created by romanb: OK, at least now you know already how to deal with test cases :)
Author
Owner

@doctrinebot commented on GitHub (Mar 16, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Mar 16, 2010): Issue was closed with resolution "Fixed"
Author
Owner

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

Imported 2 attachments from Jira into https://gist.github.com/55a4c0106618cc5460da

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

No dependencies set.

Reference: doctrine/archived-orm#504