亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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條經驗 獲得超7個贊

還需要一些其他的改變。


在我的客戶實體中,我必須更改:


? ? 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 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號