對于我的項目,我創建了 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 回答
- 0 關注
- 139 瀏覽
添加回答
舉報
0/150
提交
取消