简体   繁体   中英

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

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. But not do it

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

this only generates properties and not FK (foreing Keys), in this case @ManyToOne and @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. 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).

/**
 * @MappedSuperclass
 */
class DocumentSuperClass
{
    ...
}

/**
 * @Table(name="document")
 * @Entity(repositoryClass="AcmeBundleDocumentRepository")
 */
class Document extends DocumentSuperClass
{
    ...
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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