這個案例是一個案例研究,我正在嘗試解決這個問題,以便向我的學生解釋如何組織實體和創建表單。我的三個實體之間有這種奇異的關系:主角 <--(OneToMany)--> 事件注冊 <--(ManyToOne)--> 事件由于 EventRegistration 表中有一些列,因此無法將其轉換為多對多關系:主角:<?phpnamespace App\Entity;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\ORM\Mapping as ORM;/** * @ORM\Entity(repositoryClass="App\Repository\ProtagonistRepository") */class Protagonist{ /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=100) */ private $name; /** * @ORM\Column(type="string", length=100, nullable=true) */ private $japaneseName; /** * @ORM\Column(type="text") */ private $description; /** * @ORM\Column(type="string", length=80, nullable=true) */ private $picture; /** * @ORM\Column(type="string", length=80, nullable=true) */ private $background; /** * @ORM\Column(type="datetime", nullable=true) */ private $updated_at; /** * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="protagonists") * @ORM\JoinColumn(nullable=false) */ private $category; /** * @ORM\ManyToMany(targetEntity="App\Entity\Tag", mappedBy="protagonists") */ private $tags; /** * @ORM\OneToMany(targetEntity="App\Entity\Registration", mappedBy="protagonist") */ private $registrations; /** * @ORM\Column(type="boolean", nullable=true) */ private $isAlive; /** * @ORM\ManyToMany(targetEntity="App\Entity\Event", mappedBy="protagonists") */ private $events;
1 回答

溫溫醬
TA貢獻1752條經驗 獲得超4個贊
一些理論
實體是您的模型、具有身份、數據和行為的業務對象。
他們是核心,是您商業模式的基石。
當我們設計實體時——首先,我們應該將它們視為對象,它們有自己的形狀和職責,而不是僅僅是存儲在數據庫中的數據的容器。此外,我們應該關心實體之間的適當關系。
理想情況下,實體應始終有效。如果是這樣 - 它們可以隨時保留。
持久性是一個單獨的問題。
在一般情況下,甚至沒有必要將實體持久化到數據庫中。它們可以只保存在內存、文件系統、鍵值存儲等中。
表單也是一個單獨的關注點,它更接近于使用用戶界面。
表單幫助我們呈現用戶界面,將來自用戶的請求轉換為一些已知形狀的結構,這些結構比來自請求的原始數據更易于使用,并驗證提交的數據。
這些結構只是從請求中檢索到的數據的容器,它們不應該有任何行為。
這些結構在某些時候可能無效。
描述的問題呢?
因此,讓實體在這些形式的基礎數據結構中扮演角色可能不是最好的主意。
它只是清楚地混合了不同層之間的關注點和剛性耦合。
這就是你遇到這些問題的原因。
因此,不要將EventRegistration
類用作data_class
forEventRegistrationType
和Protagonist
- for ProtagonistType
- 考慮創建單獨的數據結構。僅在成功驗證后將提交的數據傳播到實體。
- 1 回答
- 0 關注
- 115 瀏覽
添加回答
舉報
0/150
提交
取消