Can we extend entities in Doctrine?

2019-09-04 07:02发布

问题:

I am working on my first project using Doctrine and I am stumbled on this.

  1. 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;

    }
  1. 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;
    }
  1. 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;
    }
    

回答1:

Use traits instead of inheritance.

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