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

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

ManyToMany 與 Symfony 5 的關系:不保存

ManyToMany 與 Symfony 5 的關系:不保存

PHP
慕斯王 2023-10-15 15:15:38
對于我的項目,我創建了 2 個實體:配方和成分。它們是多對多關系。 mysql 架構我從控制臺生成了所有內容(實體和 CRUD)。但是,我無法保存食譜的成分,“成分”字段不保存任何內容,只保存日期和名稱。我嘗試在我的實體中添加“cascade={”persist”}”,但它不起作用。預先感謝您的幫助 !成分.php<?phpnamespace App\Entity;use App\Repository\IngredientRepository;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\ORM\Mapping as ORM;/** * @ORM\Entity(repositoryClass=IngredientRepository::class) */class Ingredient{    /**     * @ORM\Id()     * @ORM\GeneratedValue()     * @ORM\Column(type="integer")     */    private $id;    /**     * @ORM\Column(type="string", length=255)     */    private $name;    /**     * @ORM\ManyToMany(targetEntity=Recette::class, inversedBy="ingredients",cascade={"persist"})     */    private $recette;    public function __construct()    {        $this->recette = 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|Recette[]     */    public function getRecette(): Collection    {        return $this->recette;    }    public function addRecette(Recette $recette): self    {        if (!$this->recette->contains($recette)) {            $this->recette[] = $recette;        }        return $this;    }    public function removeRecette(Recette $recette): self    {        if ($this->recette->contains($recette)) {            $this->recette->removeElement($recette);        }        return $this;    }    public function __toString()    {        return $this->name;    }}
查看完整描述

2 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

在多對多關系中,單向刪除/添加不起作用。對于這種多對多關系,您必須從兩個實體調用刪除/添加方法 $recette->addIngredient($ingredients); $Ingredients->addRecettes($recette);



查看完整回答
反對 回復 2023-10-15
?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

應該有另一個名為“ingredient_recipe”的表。多對多關系項目將保存在此處。



查看完整回答
反對 回復 2023-10-15
  • 2 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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