Doctrine2 Migration - 单表继承 - 父ID不存在

时间:2014-02-07 17:12:02

标签: symfony inheritance doctrine-orm

我没有在我的博客中使用简单的表继承。

这就是我所拥有的:

3个简单的课程

  1. 一般摘要评论类
  2. 对具有外键的博客帖子发表评论的子类
  3. 使用外键对活动发表评论的子类
  4. 一般评论实体

    /**
     * @ORM\MappedSuperClass
     * @ORM\Entity(repositoryClass="MY\BlogBundle\Entity\Repository\CommentRepository")
     * @ORM\Table(name="blog_comment")
     * @ORM\InheritanceType("SINGLE_TABLE")
     * @ORM\DiscriminatorColumn(name="type", type="string")
     * @ORM\DiscriminatorMap( {"blogentry" = "BlogComment", "activity" = "ActivityComment"} )
     * @ORM\HasLifecycleCallbacks
     */
    abstract class Comment
    {
        /**
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\Column(name="title", type="string", length=255, nullable=true)
         * @Assert\NotBlank()
         */
        private $title;
        ...
    

    评论博客

    /**
     * BlogEntryComment
     * @ORM\Entity(repositoryClass="MY\BlogBundle\Entity\Repository\BlogEntryCommentRepository")
     */
    class BlogComment extends Comment
    {
    
    /**
     * @ORM\ManyToOne(targetEntity="MY\BlogBundle\Entity\BlogEntry", inversedBy="comments")
     * @ORM\JoinColumn(name="blog_entry_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
     */
     private $blogEntry;
     ...
    

    评论活动

    /**
     * @ORM\Entity(repositoryClass="MY\BlogBundle\Entity\Repository\ActivityCommentRepository")
     */
    class ActivityComment extends Comment
    {
    /**
     * @ORM\ManyToOne(targetEntity="MY\BlogBundle\Entity\Activity", inversedBy="comments")
     * @ORM\JoinColumn(name="activity_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
     */
     private $activity;
     ...
    

    使用

    构建所有实体
    ./app/console doctrine:generate:entities MY
    

    工作正常,这意味着像getId()这样的超类方法会自动插入到Comment类中。

    子类中唯一的函数是setter和getter,用于自己的属性,如getBlogEntry()getActivity()

    当我最终尝试创建迁移以更新数据库时,我得到:

        ./app/console doctrine:migrations:diff -vvv                                                        
    
      [ReflectionException]                                                    
      Property MY\BlogBundle\Entity\ActivityComment::$id does not exist  
    
    Exception trace:
     () at MY_PATH/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php:79
     ReflectionProperty->__construct() at MY_PATH/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php:79
     Doctrine\Common\Persistence\Mapping\RuntimeReflectionService->getAccessibleProperty() at MY_PATH/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php:889
     Doctrine\ORM\Mapping\ClassMetadataInfo->wakeupReflection() at MY_PATH/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:537
     Doctrine\ORM\Mapping\ClassMetadataFactory->wakeupReflection() at MY_PATH/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:209
     Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at MY_PATH/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:114
     Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata() at MY_PATH/vendor/doctrine/migrations/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/DiffCommand.php:68
     Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand->execute() at MY_PATH/vendor/doctrine/doctrine-migrations-bundle/Doctrine/Bundle/MigrationsBundle/Command/MigrationsDiffDoctrineCommand.php:49
     Doctrine\Bundle\MigrationsBundle\Command\MigrationsDiffDoctrineCommand->execute() at MY_PATH/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:252
     Symfony\Component\Console\Command\Command->run() at MY_PATH/vendor/symfony/console/Symfony/Component/Console/Application.php:900
     Symfony\Component\Console\Application->doRunCommand() at MY_PATH/vendor/symfony/console/Symfony/Component/Console/Application.php:192
     Symfony\Component\Console\Application->doRun() at MY_PATH/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:96
     Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at MY_PATH/vendor/symfony/console/Symfony/Component/Console/Application.php:123
     Symfony\Component\Console\Application->run() at MY_PATH/app/console:22
    

    由于某些原因,它不会找到子类的id属性。

    更改访问级别导致代码创建错误。如果将$id设置为publicprotected将使用任务./app/console doctrine:generate:entities MY打破代码生成

    我知道有很多关于这个话题的帖子,但对我的问题没什么帮助。我做了:

    • 禁用所有缓存
    • 每次清除缓存甚至删除了app / cache
    • 删除所有文件并从头开始
    • 在每个子类中包含@ORM\Table(name="blog_comment")
    • 尝试了不同的访问级别修改

    有人能指出我的问题或知道可能导致这种情况的原因。我在这个令人作呕的问题上失去了整整一个工作日。

1 个答案:

答案 0 :(得分:3)

好的,首先您必须明白,将 $ id 设置为protected是唯一正确的解决方案。就是这样。

Doctrine正在所有子类中生成重复的私有 $ id ,这是真的。我在我的学说mongodb项目中遇到了同样的问题。据我所知,这是一个学说上的错误,你可以与之无关。

在每次运行generate:entities命令后,您只能删除教条在子类中生成的所有额外字段和方法。