这个案例是一个案例研究,我正在尝试解决这个问题,以便向我的学生解释如何组织实体和创建表单。我的三个实体之间有这种奇异的关系:主角 <--(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_classforEventRegistrationType和Protagonist- for ProtagonistType- 考虑创建单独的数据结构。仅在成功验证后将提交的数据传播到实体。
- 1 回答
- 0 关注
- 136 浏览
添加回答
举报
0/150
	提交
		取消
	