A
A
Aleksandr Yurchenko2020-11-25 21:06:20
symfony
Aleksandr Yurchenko, 2020-11-25 21:06:20

How to implement correct deletion of related data in Symfony?

Hello!

I can not decide in any way how to implement a prohibition on deleting a record with a warning / exception.

I have two entities

List.php
/**
* @ORM\ManyToOne (targetEntity="Download\Entity\Status", inversedBy="request")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
protected $status;
    
Status.php
/**
* @ORM\OneToMany(targetEntity="Download\Entity\List", mappedBy="status",
* cascade={"persist"}, orphanRemoval=true)
*/
protected $request;


In the above example, deleting an object from Status.php deletes all related objects from List.php. Is it possible to implement the deletion as follows: "when trying to delete the Status.php object, display a message like: 'Deletion is not possible, the object is associated with abandoned requests (requests are in the List.php table)'".

I tried to delete: @ORM\JoinColumn(onDelete="CASCADE") and cascade={"persist"}, orphanRemoval=true, but in this case, when deleting, a 500 error occurs with an exception: ailed to delete object: Download\Entity\Status.

I've been sitting here for hours and I can't figure out how to do it wisely :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2020-11-26
@yaleksandr89

You can try otherwise - make a deletion method at the level of some service, before deleting check for the presence of related entities (select /exists), if found - throw an exception with a warning. It seems to me that at the level of a standard annotation, this simply will not work, additional measures are needed)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question