A
A
anton1x2019-11-01 18:22:28
symfony
anton1x, 2019-11-01 18:22:28

What is the correct way to delete when unidirectional ManyToOne?

Hello everyone, there is the following case - there is a Media entity from the SonataMediaBundle and let's say a Foo entity that has an $image field and a ManyToOne relationship to Media. In order not to prescribe all the connections in the Media - the connection is unidirectional, only from the side of Foo.

/**
     * @ORM\ManyToOne(targetEntity="App\Application\Sonata\MediaBundle\Entity\Media")
     * @ORM\JoinColumn(name="image_id", referencedColumnName="id", nullable=true)
     */
    private $image;

Problem - when deleting some Media object through SonataAdmin, and entering the editing section for Foo, I get
"Entity of type 'App\Application\Sonata\MediaBundle\Entity\Media' for IDs id(7) was not found"
, which is quite logical since the connection is unidirectional, and the removal is not on the part of the owner.
Actually the question is, how to be, in order to do this and persistence is not violated? Is it possible somehow to solve this only with annotations for Foo? So far, the only working solution that I have found for myself is to hang an event listener on pre / postRemove for Media in which to transfer a list of all entities associated with it and manually reset the connection, but for some reason the feeling that I have washed down a wild crutch does not leave.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anton1x, 2019-11-02
@anton1x

In general, all the tinsel happened due to the use of sqlite ...
When switching to mysql, everything began to work correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question