Doctrine不会为从映射的超类继承的属性生成列

时间:2017-04-28 22:29:11

标签: symfony doctrine

我有一个映射的超类和两个扩展该基类的实体。我跟着this docs,但是当我运行bin/console doctrine:schema:update时,我的两个实体都只生成在实体本身上声明的属性,而不是在映射的超类中声明的属性。我错过了什么或做错了什么?

映射超类:

<?php

namespace AppBundle\Model;

use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\User;

/**
 * @ORM\MappedSuperclass(repositoryClass="Doctrine\ORM\EntityRepository")
 */
class Comment {
    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="comments")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $author;

    /**
     * @ORM\Column(name="content", type="text", length=500)
     */
    private $content;

    /**
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

    // setters and getters...
}

扩展实体:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AppBundle\Model\Comment;

/**
 * @ORM\Entity
 * @ORM\Table(name="component_comment")
 */
class ComponentComment extends Comment {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="ComponentComment", inversedBy="replies")
     * @ORM\JoinColumn(name="reply_to", referencedColumnName="id")
     */
    private $replyTo;

    /**
     * @ORM\OneToMany(targetEntity="ComponentComment", mappedBy="replyTo")
     */
    private $replies;

    /**
     * @ORM\ManyToOne(targetEntity="Component", inversedBy="comments")
     * @ORM\JoinColumn(name="component", referencedColumnName="id")
     */
    private $targetComponent;

    // setters and getters...

}

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AppBundle\Model\Comment;

/**
 * @ORM\Entity
 * @ORM\Table(name="food_comment")
 */
class FoodComment extends Comment {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="FoodComment", inversedBy="replies")
     * @ORM\JoinColumn(name="reply_to", referencedColumnName="id")
     */
    private $replyTo;

    /**
     * @ORM\OneToMany(targetEntity="FoodComment", mappedBy="replyTo")
     */
    private $replies;

    /**
     * @ORM\ManyToOne(targetEntity="Food", inversedBy="comments")
     * @ORM\JoinColumn(name="food", referencedColumnName="id")
     */
    private $targetFood;

    // setters and getters...
}

我的相应数据库表如下所示:

CREATE TABLE `component_comment` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `reply_to` int(11) DEFAULT NULL,
  `component` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_2478D345E2B0FBEB` (`reply_to`),
  KEY `IDX_2478D34549FEA157` (`component`),
  CONSTRAINT `FK_2478D34549FEA157` FOREIGN KEY (`component`) REFERENCES `component` (`id`),
  CONSTRAINT `FK_2478D345E2B0FBEB` FOREIGN KEY (`reply_to`) REFERENCES `component_comment` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

CREATE TABLE `food_comment` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `reply_to` int(11) DEFAULT NULL,
  `food` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_DBEB8E54E2B0FBEB` (`reply_to`),
  KEY `IDX_DBEB8E54D43829F7` (`food`),
  CONSTRAINT `FK_DBEB8E54D43829F7` FOREIGN KEY (`food`) REFERENCES `food` (`id`),
  CONSTRAINT `FK_DBEB8E54E2B0FBEB` FOREIGN KEY (`reply_to`) REFERENCES `food_comment` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

缺少从Comment父类继承的公共属性中的列。为什么我的代码有问题?

2 个答案:

答案 0 :(得分:0)

我认为您应该将映射的超类放在AppBundle / Entity

答案 1 :(得分:0)

我喜欢另一种更符合我要求的解决方案,但我想我已经想通了。我还没有对它进行测试,但问题很可能是映射的超类具有私有属性而不是受保护属性。因此,虽然它们被正确映射,但是子类既没有继承属性也没有继承属于它们的映射。