簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM