Question

I've got two entites (simplified for this question) : Folder :

class Folder {
    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="File",mappedBy="folder")
     **/
    private $files;
}

File :

class File {
    /**
     * @var Folder
     * @ORM\ManyToOne(targetEntity="Folder",inversedBy="files")
     * @ORM\JoinColumn(onDelete="CASCADE")
     **/
    private $folder;
}

I'm using database-level cascading to manage entity cascading.

I've made an event listener to be triggered when a « File » is deleted (in order to remove a MongoDB Document associated with the entity « File »)

Here that eventListener :

class MysqlMongoDBIntegrity{

    /**
     * @var DocumentManager
      */
     private $_odm;
     /**
      * @var EntityManager
      */
    private $_em;
    /**
     * @param ContainerInterface
     */
    public function setODMManager(DocumentManager $manager)
    {
        $this->_odm = $manager;
    }

    public function preRemove(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $this->_em = $args->getEntityManager();

        if ($entity instanceof File) {

            /** @var MongoDocumentRepository $mongoRepo */
            $mongoRepo = $this->_odm->getRepository('MYBundle:MongoDocument');
            $mongoRepo->removeMongoDocument($entity->getMongoDocumentId());
        }
    }
}

The event is fired as expected when removing « File » entity directly. But, when removing a « Folder » entity, the cascading specified as database-level doesn't trigger the events.

Is there any way, without changing the cascading in @JoinColumn, to trigger this event ?

I know that delegating the cascading to Doctrine (cascade={"remove"}) would be better to triggers events, but is it possible to do that without replacing cascading in @Join annotation ?

Was it helpful?

Solution

It's quite normal as explained here : in doctrine2, database level configured cascade don't raise lifecycle events, whether you use ORM or ODM. So it's not possible without replacing cascading in @Join annotations

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top