【问题标题】:ManyToMany relationship only lets add from one side Symfony多对多关系只允许从一侧添加 Symfony
【发布时间】:2021-07-25 00:41:19
【问题描述】:

我是一个全新的 Symfony 用户 - 我正在尝试与用户和实体组建立多对多关系,但是我只能从一侧(组 atm)编辑它们。挣扎了两天..真的很感激一些帮助。这是我的代码..

集团实体:

{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\ManyToMany(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;
    }

用户实体:

{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\ManyToMany(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;
    }

我正在使用 EasyAdmin 来管理这些,我的组/用户 crud 是这样的:


namespace App\Controller\Admin;

use App\Entity\Grupe;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;

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'),
        ];
    }
    
}

【问题讨论】:

    标签: php symfony doctrine many-to-many easyadmin


    【解决方案1】:

    如果您想在 Group admin 中编辑用户,我认为您正在寻找的是 CollectionField,它适用于您创建的 Symfony 表单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      相关资源
      最近更新 更多