Symfony 形式的多对多数组集合

我正在开发一个 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();

    }


慕森王
浏览 30回答 1
1回答

LEATH

还需要一些其他的改变。在我的客户实体中,我必须更改:    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方法希望它可以帮助以后其他人:)
打开App,查看更多内容
随时随地看视频慕课网APP