Merge form CollectionType with existing collection - symfony

I would need some help about management of CollectionType. In order to make my question as clear as possible, I will change my situation to fit the official Symfony documentation, with Tasks and Tags.
What I would like :
A existing task have alrady some tags assigned to it
I want to submit a list of tags with an additional field (value)
If the submitted tags are already assigned to the task->tags collection, I want to update them
It they are not, I want to add them to the collection with the submitted values
Existing tags, no part of the form, must be kept
Here is the problem :
All task tags are always overwritten by submitted data, including bedore the handleRequest method is called in the controller.
Therefore, I can't even compare the existing data using the repository, since this one already contains the collection sent by the form, even at the top of the update function in the controller.
Entity wize, this is a ManyToMany relation with an additional field (called value), so in reality, 2 OneToMany relations. Here are the code :
Entity "Task"
class Task
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\OneToMany(mappedBy: 'task', targetEntity: TaskTags::class, orphanRemoval: false, cascade: ['persist'])]
private Collection $TaskTags;
/**
* #return Collection<int, TaskTags>
*/
public function getTaskTags(): Collection
{
return $this->TaskTags;
}
public function addTaskTag(TaskTags $TaskTag): self
{
// I have voluntarily remove the presence condition during my tests
$this->TaskTags->add($TaskTag);
$TaskTag->setTask($this);
return $this;
}
public function removeTaskTag(TaskTags $TaskTag): self
{
if ($this->TaskTags->removeElement($TaskTag)) {
// set the owning side to null (unless already changed)
if ($TaskTag->getTask() === $this) {
$TaskTag->setTask(null);
}
}
return $this;
}
}
Entity "Tag"
class Tag
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\OneToMany(mappedBy: 'tag', targetEntity: TaskTags::class, orphanRemoval: false)]
private Collection $TaskTags;
/**
* #return Collection<int, TaskTags>
*/
public function getTaskTags(): Collection
{
return $this->TaskTags;
}
public function addTaskTag(TaskTags $TaskTag): self
{
$this->TaskTags->add($TaskTag);
$TaskTag->setTag($this);
return $this;
}
public function removeTaskTag(TaskTags $TaskTag): self
{
if ($this->TaskTags->removeElement($TaskTag)) {
// set the owning side to null (unless already changed)
if ($TaskTag->getTag() === $this) {
$TaskTag->setTag(null);
}
}
return $this;
}
}
Entity "TaskTags"
class TaskTags
{
#[ORM\Id]
#[ORM\ManyToOne(inversedBy: 'TaskTags')]
#[ORM\JoinColumn(nullable: false)]
private Task $task;
#[ORM\Id]
#[ORM\ManyToOne(inversedBy: 'TaskTags')]
#[ORM\JoinColumn(nullable: false)]
private Tag $tag;
// The addional field
#[ORM\Column(nullable: true)]
private ?int $value = null;
public function getTask(): ?Task
{
return $this->task;
}
public function setTask(?Task $task): self
{
if(null !== $task) {
$this->task = $task;
}
return $this;
}
public function getTag(): ?Tag
{
return $this->tag;
}
public function setTag(?Tag $tag): self
{
if(null !== $tag) {
$this->tag = $tag;
}
return $this;
}
public function getValue(): ?string
{
return $this->value;
}
public function setValue(?string $value): self
{
$this->value = $value;
return $this;
}
}
FormType "TaskFormType"
class TaskFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
...
->add('TaskTags', CollectionType::class, [
'by_reference' => false,
'entry_type' => TaskTagsFormType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Task::class,
'csrf_protection' => false
]);
}
}
FormType "TaskTagsFormType"
class TaskTagsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('task')
->add('tag')
->add('value')
;
}
Controller
#[Route('/tasks/edit/{id}/tags', name: 'app_edit_task')]
public function editasktags(Request $request, EntityManagerInterface $em, TaskTagsRepository $TaskTagsRepo): Response
{
...
// Create an ArrayCollection of the current tags assigned to the task
$task = $this->getTask();
// when displaying the form (method GET), this collection shows correctly the tags already assigned to the task
// when the form is submitted, it immediately becomes the collection sent by the form
$ExistingTaskTags = $TaskTagsRepo->findByTask($task);
$form = $this->createForm(TaskFormType::class, $task);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// here begins all I have tried ... I was trying to compare the value in the DB and in the form, but because of the repo being overwritten I can't
$task = $form->getData();
$SubmittedTaskTags = $userForm->getTaskTags();
$CalculatedTaskTags = new ArrayCollection();
foreach ($ExistingTaskTags as $ExistingTaskTag) {
foreach ($SubmittedTaskTags as $SubmittedTaskTag) {
if ($ExistingTaskTag->getTag()->getId() !== $SubmittedTaskTag->getTag()->getId()) {
// The existing tag is not the same as submitted, keeping it as it in a new collection
$CalculatedTaskTags->add($ExistingTaskTag);
} else {
// The submitted tag is equal to the one in DB, so adding the submitted one
$SubmittedTaskTag->setTask($task);
$CalculatedTaskTags->add($SubmittedTaskTag);
}
}
}
$em->persist($task);
$em->flush();
}
return $this->render('task/edittasktags.twig.html', [
'form' => $form,
'task' => $this->getTask()
]);
}
My main issue is that I am not able to get the existing data one the form has been submitted, in order to perform a "merge"
I have tried so many things.
One I did not, and I'd like to avoid : sending the existing collection as hidden fields.
I don't like this at all since if the data have been modified in the meantime, we are sending outdated data, which could be a mess in multi tab usage.
Thank you in advance for your help, I understand this topic is not that easy.
NB : the code I sent it not my real code / entity. I've re written according to the Symfony doc case, so there could be some typo here and there, apologize.

Solution found.
I added 'mapped' => false in the FormType.
And I was able to retrieve the form data using
$SubmittedTags = $form->get('TaskTags')->getData();
The repository was not overwritten by the submitted collection.

Related

#[ApiResource] attribute causing exception in OneToMay resource

I have two related doctrine entities:
Content
#[ApiResource(operations: [new Get(), new Patch(), new Delete(), new Put(), new Post(uriTemplate: '/contents/add_text', controller: ContentTextPersist::class, read: false, openapiContext: ['description' => 'This endpoint provides a way to add content text and variants to a content object without incurring circular reference issue as a result of the nested nature of the Content -> ContentText and ContentTextVariants.', 'summary' => 'Persist new content text item and it\'s translations.']), new Post(), new GetCollection()], order: ['createdAt' => 'DESC'], normalizationContext: ['groups' => ['content:read']], denormalizationContext: ['groups' => ['content:write']], filters: ['translation.groups'])]
#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
class Content extends AbstractTranslatable
{
....
/**
* #var \Doctrine\Common\Collections\Collection<int, \App\Entity\ContentText>|\App\Entity\ContentText[]
*/
#[ORM\OneToMany(targetEntity: ContentText::class, mappedBy: 'content', orphanRemoval: true, cascade: ['persist'])]
#[Groups(['content:read', 'content:write'])]
private iterable $text;
...
public function __construct()
{
parent::__construct();
$this->text = new ArrayCollection();
}
...
/**
* #return Collection|ContentText[]
*/
public function getText() : Collection
{
return $this->text;
}
public function addText(ContentText $text) : self
{
if (!$this->text->contains($text)) {
$this->text[] = $text;
$text->setContent($this);
}
return $this;
}
public function removeText(ContentText $text) : self
{
if ($this->text->removeElement($text)) {
// set the owning side to null (unless already changed)
if ($text->getContent() === $this) {
$text->setContent(null);
}
}
return $this;
}
}
ContentText
#[ApiResource(operations: [new Patch(), new Delete(), new Get(), new GetCollection()], order: ['createdAt' => 'DESC'], paginationPartial: true, normalizationContext: ['groups' => ['contentText:read']], denormalizationContext: ['groups' => ['contentText:write']], filters: ['translation.groups'])]
#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
class ContentText
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER)]
#[Groups(['contentText:read', 'content:read'])]
private ?int $id = null;
public function getId() : ?int
{
return $this->id;
}
}
Would anyone know why if I remove #[ApiResource] attribute from ContentText entity then I'm able to get the collection of ContentText related to Content otherwise I get the error below:
"Unable to generate an IRI for the item of type \"App\\Entity\\ContentText\""

OneToMany relationship issues

I have two entities, PartenairePermission and StructurePermission, I'm trying to get properties from the other entity in a One To Many relationship.
Which was working great with the one to one relationship but I modified it for a One To Many relationship and now I can't access the properties anymore
Attempted to call an undefined method named "setIsMembersRead" of class "Doctrine\ORM\PersistentCollection".
The idea of the script below is when the property is modified in PartenairePermission, it modified the property in StructurePermission too.
Any idea on how to solve this issue?
PartenaireController: [EDITED]
#[Route('/{id}/activate-permission', name: 'app_partenaire_activate-permission', methods: ['GET', 'POST'])]
public function activatePermission(EntityManagerInterface $entityManager, Request $request, PartenaireRepository $partenaireRepository, MailerInterface $mailer): Response
{
$partenairePermission = $entityManager->getRepository(PartenairePermission::class)->findOneBy([ // get the id of the partenaire
'id' => $request->get('id'),
]);
$partenairePermission->setIsMembersRead(!$partenairePermission->isIsMembersRead()); // set the value of the permission to the opposite of what it is ( for toggle switch )
$structurePermission = $partenairePermission->getPermissionStructure();
foreach ($structurePermission as $structurePermission) {
$structurePermission->setIsMembersRead($partenairePermission->isIsMembersRead());
}
$entityManager->persist($partenairePermission);
$entityManager->flush();
PartenairePermission.php :
#[ORM\OneToMany(mappedBy: 'permission_partenaire', targetEntity: StructurePermission::class, orphanRemoval: true)]
private Collection $permission_structure;
public function __construct()
{
$this->permission_structure = new ArrayCollection();
} /**
* #return Collection<int, StructurePermission>
*/
public function getPermissionStructure(): Collection
{
return $this->permission_structure;
}
public function addPermissionStructure(StructurePermission $permissionStructure): self
{
if (!$this->permission_structure->contains($permissionStructure)) {
$this->permission_structure->add($permissionStructure);
$permissionStructure->setPermissionPartenaire($this);
}
return $this;
}
public function removePermissionStructure(StructurePermission $permissionStructure): self
{
if ($this->permission_structure->removeElement($permissionStructure)) {
// set the owning side to null (unless already changed)
if ($permissionStructure->getPermissionPartenaire() === $this) {
$permissionStructure->setPermissionPartenaire(null);
}
}
return $this;
}
StructurePermission.php :
#[ORM\ManyToOne(fetch: "EAGER", inversedBy: 'permission_structure')]
#[ORM\JoinColumn(nullable: false)]
private ?PartenairePermission $permission_partenaire = null;
public function getPermissionPartenaire(): ?PartenairePermission
{
return $this->permission_partenaire;
}
public function setPermissionPartenaire(?PartenairePermission $permission_partenaire): self
{
$this->permission_partenaire = $permission_partenaire;
return $this;
}
Now you have to work different since you changed the association type:
$structurePermission = $partenairePermission->getPermissionStructure();
this will return a Collection (instead of a single Object as with your former One-to-One relationship).
and then something like:
foreach($structurePermission as $permission) {
// here you call your set/get/is for an Object within the Collection
}

Symfony 5 easyadmin 3 Entity with relation ManyToOne - NOT saving on the "many" side

I have a very basic symfony 5 + easyadmin 3 app.
I created using the make:entity two entities: Posts and Categories
When I try to edit Category to assign Posts, posts are not saved in DB.
But If I add the category on the post edit is saves in db.
Any idea what I'm missing here?
CategoryCrudController.php
public function configureFields(string $pageName): iterable
{
if (Crud::PAGE_EDIT === $pageName)
{
yield TextField::new('title');
yield DateTimeField::new('created_at')
->setFormTypeOption('disabled','disabled');
yield AssociationField::new('posts')
->autocomplete();
Entity Category.php
/**
* #ORM\OneToMany(targetEntity=Post::class, mappedBy="category")
*/
private $posts;
public function __construct()
{
$this->posts = new ArrayCollection();
}
/**
* #return Collection|Post[]
*/
public function getPosts(): Collection
{
return $this->posts;
}
public function addPost(Post $post): self
{
if (!$this->posts->contains($post)) {
$this->posts[] = $post;
$post->setCategory($this);
}
return $this;
}
public function removePost(Post $post): self
{
if ($this->posts->removeElement($post)) {
// set the owning side to null (unless already changed)
if ($post->getCategory() === $this) {
$post->setCategory(null);
}
}
return $this;
}
Found the solution thanks to:
https://github.com/EasyCorp/EasyAdminBundle/issues/860#issuecomment-192605475
For Easy Admin 3 you just need to add
->setFormTypeOptions([
'by_reference' => false,
])
CategoryCrudController.php
public function configureFields(string $pageName): iterable
{
if (Crud::PAGE_EDIT === $pageName)
{
yield TextField::new('title');
yield DateTimeField::new('created_at')
->setFormTypeOption('disabled','disabled');
yield AssociationField::new('posts')
->setFormTypeOptions([
'by_reference' => false,
])
->autocomplete();

Doctrine - Get old value in onFlush event listener

I'm trying to implement a feature that let's the user specify his mutual status and select a another user, just like that one in Facebook.
My database schema is like:
UserInfo entity:
class UserInfo
{
/**
* #ORM\OneToOne(targetEntity="User", inversedBy="user_info")
*/
protected $user;
/**
* #ORM\Column(type="text", nullable=true)
* #Assert\NotBlank
*/
protected $status; //Value taken from a dropdown list
/**
* #ORM\OneToOne(targetEntity="User", inversedBy="relationship_with_table")
*/
protected $relationship_user;
}
User entity:
class User
{
/**
* #ORM\OneToOne(targetEntity="UserInfo", mappedBy ="user", cascade={"persist","remove"})
*/
protected $user_info;
/**
* #ORM\OneToOne(targetEntity="UserInfo", mappedBy="relationship_user")
*/
protected $relationship_with_table;
}
UserInfoType :
class UserInfoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $option)
{
$builder
->add("birthday", "birthday")
->add("gender", "choice", array("choices" => array("0" => "Male", "1" => "Female")))
->add("status", "choice", array("choices" => array(
"Single" => "Single",
"In relationship" => "In relationship",
"Engaged" => "Engaged",
"Married" => "Married"
)))
->add("relationship_user", "thrace_select2_entity", array(
"class" => 'Zgh\FEBundle\Entity\User',
'label' => 'User',
'empty_value' => 'Select user',
"configs" => array(
"width" => '100%',
),
))
->add("city", "text")
->add("work", "text")
->add("facebook", "url")
->add("twitter", "url")
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
"data_class" => 'Zgh\FEBundle\Entity\UserInfo',
"cascade_validation" => true,
));
}
public function getName()
{
return "user_info";
}
}
Now here's the event listener, It checks the user status and change the other user one, It works as expected. But only has one flaw, If the user is married to (A) and then changed it to married to (B), The user (A) still gets the Married to and don't get reset.
All what i want to do is before attaching user relationship to (B), I want to retrieve (A) and reset it. How can this be done inside onFlush event.
class DoctrinePreUpdateHandler implements EventSubscriber
{
public function getSubscribedEvents()
{
return array(
"onFlush",
);
}
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$updates = $uow->getScheduledEntityUpdates();
foreach ($updates as $entity) {
if($entity instanceof UserInfo){
$target_user_info = null;
if($entity->getStatus() == "Single"){
if($entity->getRelationshipUser() != null){
$target_user_info = $entity->getRelationshipUser()->getUserInfo();
$target_user_info->setStatus("Single");
$target_user_info->setRelationshipUser(null);
}
$entity->setRelationshipUser(null);
} else {
$target_user_info = $entity->getRelationshipUser()->getUserInfo();
$target_user_info->setStatus($entity->getStatus());
$target_user_info->setRelationshipUser($entity->getUser());
}
if($target_user_info != null){
$em->persist($target_user_info);
$uow->computeChangeSet($em->getClassMetadata(get_class($target_user_info)), $target_user_info);
}
}
}
}
}
Doctrine UoW has a method propertyChanged(...) that tracks changes to entity properties.
These changes are stored in the entityChangeSets-Property of the UoW.
I think it should be possible to call $uow->getEntityChangeSet($entity) which returns an array where keys are $entity's properties and values are [$oldValue, $newValue].
I'm not sure if you have to call computeChangeSet() for the old relationship user, but i hope you can figure this out and leave a comment?

Accessing a form field from a subscriber (of a form event) in Symfony2

I'm following the tutorial How to Dynamically Generate Forms Using Form Events. I'm stuck on the creation of AddNameFieldSubscriber:
$subscriber = new AddNameFieldSubscriber($builder->getFormFactory());
My question is simple: how FormFactory can access and modify an arbitrary form field previously created by the $builder? And why we are passing the FormFactory instead of the $builder itself?
Assuming we have just two fields ("name" and "price") in the builder:
class ProductType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$subscriber = new AddProductTypeSubscriber($builder->getFormFactory());
$builder->addEventSubscriber($subscriber);
$builder->add('name');
$builder->add('price');
}
public function getName() { return 'product'; }
}
I'd like to set required = false (just an example) in the subscriber:
class ProductTypeSubscriber implements EventSubscriberInterface
{
private $factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_SET_DATA => 'preSetData');
}
public function preSetData(DataEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) return;
// Access "name" field and set require = false
}
}
I could be wrong about it this, but I don't believe you can change a form's attributes after its been created. However, you can add to the form.
Instead of adding the 'name' field in ProductType::buildForm, you can defer this to the subscriber:
if (!$data->getId()) {
$form->add($this->factory->createNamed('text', 'name', null, array('required' => false)));
} else {
$form->add($this->factory->createNamed('text', 'name'));
}

Resources