Pergunta

I have the following problem with clearing entities in doctrine:
I have two entities connected together, one of which is the main-entity so to say and one of which is the sub-entity that belongs to the main-entity. Then when I use the according repository to clear, it only clears the main-entity but leaves the sub-entity.
Flushing the entities is actually working fine, because the entities are connected via cascade, so when I flush the main entity, the sub-entity gets flushed also. But this cascading does not seem to work with the clear.
Is there a way to also clear all the sub-entites together with the main entities without creating an extra (and actually not needed for other things) repository for the sub-entities? Thank you in advance.

EDIT: Ok since I've seemingly been too unspecific, my goal is to clear the entities in doctrine, not delete them in the database. The problem is, that I have a lot of entities to process and doctrine doesn't clear up all entity references in the memory. So is there a way to cascade that, or do I need the repositories for that?

LAST EDIT: Problem has been fixed by doctrine. See accepted answer!

Foi útil?

Solução

As of the last commit, the issue is fixed. Now cascade clearing works like a charm for me. Thanks a lot to the developers of doctrine!

Additional info, see here: https://github.com/doctrine/doctrine2/pull/995#issuecomment-40562699 https://github.com/doctrine/doctrine2/commit/1cd0b26a40dc22b0d11b1860eb058ab9cdc29f36

Outras dicas

You need to add cascade={"remove"} to your mapping information in the main entity for field that represent sub-entity.

You can use cascade={"remove"} for each main-entity and sub-entity; it will be do the removal for children on memory which may lead to performance overhead.
If you want to rely on DB for removal which is faster and reliable you can configure each column on delete event onDelete="CASCADE"
(however you did not provide any code but as example see below)

/**
* @ORM\OneToMany(targetEntity="SubEntity", mappedBy="mainEntity")
*/
protected $subEntiy;

and in the other entity you have

/**
* @ORM\ManyToOne(targetEntity="MainEntity", inversedBy="subEntity")
* @ORM\JoinColumn(name="entity_id", referencedColumnName="id",  onDelete="CASCADE")
*/
protected $mainEntity;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top