我们可以扩展学说中的实体吗? [英] Can we extend entities in Doctrine?
问题描述
我正在使用Doctrine进行我的第一个项目,结果迷迷糊糊。
I am working on my first project using Doctrine and I am stumbled on this.
-
我有一个实体User,是映射的超类。
I have an entity User which is a mapped super class.
/**
* @ORM\MappedSuperclass
*/
abstract class User
{
/**
* @var int
*/
protected $id;
/**
* @var string
*/
protected $name;
/**
* @ORM\Embedded(class="AppBundle\Entity\Person")
* @var Person
*/
protected $person;
/**
* @var Authors[]|Collection|Selectable
*/
protected $authors;
}
我创建了一个作者来自上述用户实体的实体
I create an author entity from the User entity above
class Author extends User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @var int
*/
protected $id;
/**
* @ORM\JoinColumn(name="publisher_id", referencedColumnName="id", nullable=false, unique=true)
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Publisher", inversedBy="book")
* @var Publisher
*/
protected $publisher;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Publisher", mappedBy="author")
* @var Approval[]|Collection|Selectable
*/
protected $approvals;
}
-
我使用Author实体创建对象实体
I create an object entity Book using the Author entity
/**
* @ORM\Entity
* @ORM\Table(name="magazine", options={"collate":"utf8_general_ci"})
*/
class Book
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @var int
*/
protected $id;
/**
* @ORM\JoinColumn(name="publisher_id", referencedColumnName="id", nullable=false, unique=true)
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Publisher", inversedBy="book")
* @var Publisher
*/
protected $publisher;
}
和我的发布商实体如下所示:
And my Publisher entity is as below:
/**
* @ORM\Entity
* @ORM\Table(name="ops_approval", options={"collate":"utf8_general_ci"})
*/
class Publisher
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @var int
*/
protected $id;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
protected $createdAt;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Author", inversedBy="authors")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
*/
protected $author;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Book", mappedBy="book"
*/
protected $book;
}
-
现在,我可以扩展发布的实体来创建Book实体
Now, can I extend published entity to create a Book entity
/**
* @ORM\Entity
* @ORM\Table(name="book", options={"collate":"utf8_general_ci"})
*/
class Book extends Magazine
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @var int
*/
protected $id;
/**
* @ORM\JoinColumn(name="published_id", referencedColumnName="id", nullable=false, unique=true)
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Publisher", inversedBy="book")
* @var Publisher
*/
protected $publisher;
}
推荐答案
U
如果需要,您还可以覆盖某些映射:
http://doctrine-orm.readthedocs.io/projects/ doctrine-orm / en / latest / tutorials / override-field-association-mappings-in-subclasses.html
You can also override some mappings if needed:
http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/tutorials/override-field-association-mappings-in-subclasses.html
这篇关于我们可以扩展学说中的实体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!