简体   繁体   English

原则不更新/生成ManyToOne和OneToMany的字段

[英]Doctrine doesn't update/generate fields of ManyToOne and OneToMany

I have a superclass that currently works fine (all relations and properties are updating to the database) 我有一个目前可以正常工作的超类(所有关系和属性都正在更新到数据库)

    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 */

}

Now I've created a entity called Note that extends to Document entity 现在,我创建了一个名为Note的实体,该实体扩展到Document实体

use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Entity;

    /**
     * Note
     *
     * @Table(name="note")
     * @Entity(repositoryClass="NoteRepository")
     */
    class Note extends Document
    {

    }

I am suppose that the table/entity note should generate the same things of the class that extends. 我想表/实体note应该生成与扩展类相同的东西 But not do it 但是不做

I run php bin/console doctrine:schema:update -f 我运行php bin/console doctrine:schema:update -f

this only generates properties and not FK (foreing Keys), in this case @ManyToOne and @OneToMany . 这只会生成属性,而不生成FK(在键之前),在这种情况下为@ManyToOne@OneToMany

Additionally maybe help us, i have those entities on the same database 另外也许可以帮助我们,我在同一数据库中拥有这些实体

I am doing something wrong ? 我做错了什么?

As per docs I think you're missing the @MappedSuperclass annotation or you're using Doctrine inheritance in the wrong way. 根据文档,我认为您缺少@MappedSuperclass注释,或者您以错误的方式使用了Doctrine继承。 Be aware that a MappedSupperClass is not an entity by itself instead is just a class for share common methods and properties among it is children classes (same inheritance concept that you should already know). 请注意, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM