ManyToMany relationship only lets add from one side Symfony

  doctrine, easyadmin, many-to-many, php, symfony

I’m a totally new Symfony user – I’m trying to make a ManyToMany relationship with User and Entity Group, however i can edit those only from one side(Group atm). Struggling with this for two days.. Would really appreciate some help. Here is my code..

Group Entity:

{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=160)
     */
    private $name;

    /**
     * @ORMManyToMany(targetEntity=Vartotojas::class, inversedBy="groups")
     */
    private $users;

    public function __construct()
    {
        $this->users = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Vartotojas[]
     */
    public function getUsers(): Collection
    {
        return $this->users;
    }

    public function addUser(Vartotojas $user): self
    {
        if (!$this->users->contains($user)) {
            $this->users[] = $user;
            $user->addGroup($this);
        }

        return $this;
    }

User Entity:

{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=64)
     */
    private $name;

    /**
     * @ORMManyToMany(targetEntity=Grupe::class, inversedBy="users")
     */
    private $groups;

    public function __construct()
    {
        $this->groups = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Grupe[]
     */
    public function getGroups(): Collection
    {
        return $this->groups;
    }

    public function addGroup(Grupe $group): self
    {
        if (!$this->group->contains($group)) {
            $this->groups[] = $group;
            $group->addUser($this);
            
        }

        return $this;
    }

I’m using EasyAdmin to manage those, and my Group/User crud’s are the same like this:


namespace AppControllerAdmin;

use AppEntityGrupe;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldTextField;
use EasyCorpBundleEasyAdminBundleFieldAssociationField;

class GrupeCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Grupe::class;
    }

    
    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('name'),
            AssociationField::new('users'),
        ];
    }
    
}

Source: Ask PHP

LEAVE A COMMENT