为了账号安全,请及时绑定邮箱和手机立即绑定

Symfony 形式的多对多数组集合

Symfony 形式的多对多数组集合

PHP
慕森王 2024-01-19 10:16:50
我正在开发一个 Symfony 项目,其中有两种类型的 User Client 和 EmployeSpie,两者都有自己的实体。当您创建/编辑用户时,您可以将 EmployeSpie 链接到客户端。这就是我的问题所在,当我编辑或创建用户时,我可以创建一个用户,但我的表中没有存储任何内容来建立我的表 Client 和 EmployeSpie 之间的链接。这是我所做的:我的实体客户有这样的:class Client extends User{    /**     * @ORM\ManyToMany(targetEntity=EmployeSpie::class, mappedBy="clients", cascade={"persist"})     */    private $employeSpies;     /**     * @return Collection|EmployeSpie[]     */    public function getEmployeSpies(): Collection    {        return $this->employeSpies;    }    public function addEmployeSpy(EmployeSpie $employeSpy): self    {        if (!$this->employeSpies->contains($employeSpy)) {            $this->employeSpies[] = $employeSpy;            $employeSpy->addClientEmploye($this);        }        return $this;    }    public function removeEmployeSpy(EmployeSpie $employeSpy): self    {        if ($this->employeSpies->contains($employeSpy)) {            $this->employeSpies->removeElement($employeSpy);            $employeSpy->removeClientEmploye($this);        }        return $this;    }}和我的表 EmployeSpie:class EmployeSpie extends User{    /**     * @ORM\ManyToMany(targetEntity=Client::class, inversedBy="employeSpies")     */    private $clients;    /**     * @return Collection|Client[]     */    public function getClients(): Collection    {        return $this->clients;    }    public function addClient(Client $client): self    {        if (!$this->clients->contains($client)) {            $this->clients[] = $client;        }        return $this;    }    public function removeClient(Client $client): self    {        if ($this->clients->contains($client)) {            $this->clients->removeElement($client);        }        return $this;    }    public function __toString()    {        return $this->getPrenom()." ".$this->getNom();    }
查看完整描述

1 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

还需要一些其他的改变。


在我的客户实体中,我必须更改:


    public function addEmployeSpy(EmployeSpie $employeSpy): self

    {

        if (!$this->employeSpies->contains($employeSpy)) {

            $this->employeSpies[] = $employeSpy;

            $employeSpy->addClientEmploye($this);

        }


        return $this;

    }

到 :


    public function addEmployeSpy(EmployeSpie $employeSpy): self

    {

        if (!$this->employeSpies->contains($employeSpy)) {

            $this->employeSpies[] = $employeSpy;

            $employeSpy->addClient($this);

        }


        return $this;

    }

删除也是同样的事情。


public function removeEmployeSpy(EmployeSpie $employeSpy): self

    {

        if ($this->employeSpies->contains($employeSpy)) {

            $this->employeSpies->removeElement($employeSpy);

            $employeSpy->removeClientEmploye($this);

        }


        return $this;

    }

到 :


public function removeEmployeSpy(EmployeSpie $employeSpy): self

    {

        if ($this->employeSpies->contains($employeSpy)) {

            $this->employeSpies->removeElement($employeSpy);

            $employeSpy->removeClient($this);

        }


        return $this;

    }

但在我的 ClientType 进行其他更改之后:


            ->add('employeSpies', EntityType::class, array(

                'class' => EmployeSpie::class ,

                'by_reference' => false,

                'label'     => 'Sélectionnez les employés rattachés à ce client',

                'expanded'  => false,

                'multiple'  => true,

            ))

我需要添加'by_reference' => false,才能使其正常工作。


因此,Symfony 不会尝试查找“setClient”方法,而是查找addClient方法


希望它可以帮助以后其他人:)


查看完整回答
反对 回复 2024-01-19
  • 1 回答
  • 0 关注
  • 25 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信