[英]Doctrine doesn't update/generate fields of ManyToOne and OneToMany
我有一个目前可以正常工作的超类(所有关系和属性都正在更新到数据库)
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\JoinColumn;
use JMS\Serializer\Annotation 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)
* @JMS\Exclude()
*/
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 Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Entity;
/**
* Note
*
* @Table(name="note")
* @Entity(repositoryClass="NoteRepository")
*/
class Note extends Document
{
}
我想表/实体note
应该生成与扩展类相同的东西 。 但是不做
我运行php bin/console doctrine:schema:update -f
这只会生成属性,而不生成FK(在键之前),在这种情况下为@ManyToOne
和@OneToMany
。
另外也许可以帮助我们,我在同一数据库中拥有这些实体
我做错了什么?
根据文档,我认为您缺少@MappedSuperclass
注释,或者您以错误的方式使用了Doctrine继承。 请注意, MappedSupperClass
本身并不是一个实体,而只是一个用于共享通用方法和属性的类,而子类是该类(您应该已经知道的相同继承概念)。
/**
* @MappedSuperclass
*/
class DocumentSuperClass
{
...
}
/**
* @Table(name="document")
* @Entity(repositoryClass="AcmeBundleDocumentRepository")
*/
class Document extends DocumentSuperClass
{
...
}
/**
* @Table(name="note")
* @Entity(repositoryClass="NoteRepository")
*/
class Note extends DocumentSuperClass
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.