EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Add/Update/Remove AssociationField

Open devcut opened this issue 6 years ago • 11 comments

Hello,

I have a entity with AssociationField and when i submitted the form, but nothing changes... the getters and setters are never called.

$usersPrimary = AssociationField::new('usersPrimary');
$usersSecondary = AssociationField::new('usersSecondary');

My entity:


/**
 * @ORM\OneToMany(targetEntity=User::class, mappedBy="clientPrimary")
 */
private $usersPrimary;

/**
 * @ORM\ManyToMany(targetEntity=User::class, mappedBy="clientsSecondary")
 */
private $usersSecondary;

/**
 * @return Collection|User[]
 */
public function getUsersPrimary(): Collection
{
    return $this->usersPrimary;
}

public function addUsersPrimary(User $usersPrimary): self
{
    if (!$this->usersPrimary->contains($usersPrimary)) {
        $this->usersPrimary[] = $usersPrimary;
        $usersPrimary->setClientPrimary($this);
    }

    return $this;
}

public function removeUsersPrimary(User $usersPrimary): self
{
    if ($this->usersPrimary->contains($usersPrimary)) {
        $this->usersPrimary->removeElement($usersPrimary);
        // set the owning side to null (unless already changed)
        if ($usersPrimary->getClientPrimary() === $this) {
            $usersPrimary->setClientPrimary(null);
        }
    }

    return $this;
}

/**
 * @return Collection|User[]
 */
public function getUsersSecondary(): Collection
{
    return $this->usersSecondary;
}

public function addUsersSecondary(User $usersSecondary): self
{
    if (!$this->usersSecondary->contains($usersSecondary)) {
        $this->usersSecondary[] = $usersSecondary;
        $usersSecondary->addClientsSecondary($this);
    }

    return $this;
}

public function removeUsersSecondary(User $usersSecondary): self
{
    if ($this->usersSecondary->contains($usersSecondary)) {
        $this->usersSecondary->removeElement($usersSecondary);
        $usersSecondary->removeClientsSecondary($this);
    }

    return $this;
}

devcut avatar Nov 02 '20 11:11 devcut

I have same problem - OneToMany association does not update other side entity (ManyToOne)

zorn-v avatar Dec 29 '20 13:12 zorn-v

I also encounter the problem with version 3.2.2 of EasyAdmin and Symfony 5.2

Octen avatar Jan 25 '21 10:01 Octen

Same issue : https://github.com/EasyCorp/EasyAdminBundle/issues/4173

valentin-harrang avatar Feb 01 '21 14:02 valentin-harrang

same here :(

tricsusz avatar Feb 03 '21 21:02 tricsusz

Same issue for me. I've tested a ManyToMany relation. I can't update my datas from one side, but there is no update from the other one (same methods for my two entities)

bargoud avatar Feb 17 '21 16:02 bargoud

Same issue for me.

nicosomb avatar Mar 31 '21 09:03 nicosomb

Thanks to @mauchede, it works.

You need to add ->setFormTypeOption('by_reference', false) on your AssociationField.

Without that, the setter is not called.

nicosomb avatar Mar 31 '21 09:03 nicosomb

Thank you so much @nicosomb .

adpeyre avatar Apr 01 '21 16:04 adpeyre

You need to add ->setFormTypeOption('by_reference', false) on your AssociationField.

@javiereguiluz I think it should be added to docs. Actually I thought that it is something like "not implemented yet" and do changes in my project related to it ("many" side was not so important to me).

zorn-v avatar Apr 01 '21 16:04 zorn-v

If some of you understand Symfony Forms well ... what do you think about the following idea?


Should we set by_reference to false by default in AssociationField when the user hasn't defined the by_reference explicitly?


Thanks!

javiereguiluz avatar Jan 03 '22 19:01 javiereguiluz

This issue has been automatically closed as part of our end-of-year repository maintenance. As an open-source project with limited resources, we need to focus our efforts on the most current and impactful issues. If this feature or bug is still relevant to you, please create a new issue. For bugs, include a detailed reproducer, and for features, provide a clear explanation of your request. Thank you for your understanding and for supporting our project!

javiereguiluz avatar Dec 30 '24 21:12 javiereguiluz