我有一个当前工作正常的超类(所有关系和属性都更新到数据库(
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMappingColumn;
use DoctrineORMMappingTable;
use DoctrineORMMappingEntity;
use DoctrineORMMappingId;
use DoctrineORMMappingGeneratedValue;
use DoctrineORMMappingManyToOne;
use DoctrineORMMappingOneToMany;
use DoctrineORMMappingJoinColumn;
use JMSSerializerAnnotation as JMS;
/**
* Document
*
* @Table(name="document")
* @Entity(repositoryClass="AcmeBundleDocumentRepository")
*/
class Document
{
/**
* @var string
*
* @Column(name="id", type="string")
* @Id
* @GeneratedValue(strategy="UUID")
*/
protected $id;
/**
* @var string
* @Column(name="name", type="string", length=255)
*/
protected $name;
/**
* @var string
* @Column(name="type", type="string", length=255)
*/
protected $type;
/**
* @var boolean
* @Column(name="has_attachments", type="boolean")
*/
protected $hasAttachments;
/**
* @ManyToOne(targetEntity="Delivery")
* @JoinColumn(name="delivery_id", referencedColumnName="id", nullable=false)
* @JMSExclude()
*/
protected $delivery;
/**
* @OneToMany(targetEntity="Extension", mappedBy="document", cascade={"persist","remove"})
**/
protected $extensions;
public function __construct()
{
$this->extensions = new ArrayCollection();
}
/* getter and setters */
}
现在,我已经创建了一个名为Note
的实体,该实体扩展到Document
实体
use DoctrineORMMappingTable;
use DoctrineORMMappingEntity;
/**
* Note
*
* @Table(name="note")
* @Entity(repositoryClass="NoteRepository")
*/
class Note extends Document
{
}
我假设表/实体note
应该生成扩展类的相同内容。但不做
我跑php bin/console doctrine:schema:update -f
这仅生成属性而不是 FK(前键(,在本例中为@ManyToOne
和@OneToMany
.
此外,也许可以帮助我们,我将这些实体放在同一个数据库中
我做错了什么?
根据文档,我认为您缺少@MappedSuperclass
注释,或者您以错误的方式使用教义继承。请注意,MappedSupperClass
本身不是一个实体,而只是一个类,用于共享公共方法,并且它之间的属性是子类(您应该已经知道相同的继承概念(。
/**
* @MappedSuperclass
*/
class DocumentSuperClass
{
...
}
/**
* @Table(name="document")
* @Entity(repositoryClass="AcmeBundleDocumentRepository")
*/
class Document extends DocumentSuperClass
{
...
}
/**
* @Table(name="note")
* @Entity(repositoryClass="NoteRepository")
*/
class Note extends DocumentSuperClass
{
...
}