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

Open
opened 2026-01-22 12:40:37 +01:00 by admin · 0 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:37 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#503