A
A
AlexKuznec2018-01-25 18:40:00
symfony
AlexKuznec, 2018-01-25 18:40:00

Symfony removes unnecessary ManyToMany associations when using a filter. How to win?

I'm using Symfony 3.3
. I have 2 tables (Entity) User and Project with a standard relationship
@ORM\ManyToMany
In the standard form of the standard controller, the user can select checkboxes to have relationships between User and the list of filtered Projects (not all are displayed).
For filtering I use the successor SQLFilter (the same filter uses SoftDeleteable).
The problem is that if you uncheck the last checkbox, then all links from the database are deleted, including those that were filtered and were not displayed in the list.
This query is launched: instead of the usual query
DELETE FROM user_project WHERE user_id = ?

DELETE FROM user_project WHERE user_id = ? AND project_id = ?

An attempt to somehow influence the process led to testing what was happening through the use of the form
$builder->get('projects')
            ->addModelTransformer(new CallbackTransformer(...

It receives data from the \Doctrine\ORM\PersistentCollection class containing my Project objects.
There are 2 useful methods there:
getSnapshot() - returns an array of objects before the change
getDeleteDiff() - an array of objects to delete (the difference between the current state and Snapshot) The
trick is that when the last element is deleted, they both turn out to be empty. How?
That is, the behavior is as follows:
Number of links: Snapshot elements: getDeleteDiff() elements:
3 -> 2 .............................. ............ 3 .............................. ........... 1
2 -> 1 .................................. ........ 2 ........................................ ....... one
1 -> 0 ........................................ 0 ... ............................................... 0
That is, I even the deleted object cannot be used to rebuild the collection, as it has disappeared without a trace!
Ideally, I would like to add a filter to the DELETE query (it is automatically added to SELECT, but not to DELETE), even if somehow manually.
Well, or at least prevent the deletion of elements without their numbers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexKuznec, 2018-01-25
@AlexKuznec

It was solved very simply)
It was necessary to set the form property 'by_reference' => false, after which the removal method from my Entity (passed to the form) began to be used, and not automatically. Moreover, this property, according to the documentation, should seem to influence the use of the methods of the associated entity, and not the one passed to the form. Instead of PersistentCollection, an ArrayCollection was passed to the transformer, apparently from my getter.
There is a mention of this here (the main topic is that relationships are not saved):
https://afilina.com/doctrine-not-saving-manytomany
However, the issue of attaching a filter to a DELETE request is still relevant. To make sure your data is protected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question