gpt4 book ai didi

symfony - Doctrine2迁移-单表继承-父id不存在

转载 作者:行者123 更新时间:2023-12-02 14:08:55 25 4
gpt4 key购买 nike

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

这是我所拥有的:

3个简单的类

  1. 通用抽象评论类
  2. 对带有外键的博客文章发表评论的子类
  3. 带有外键的事件评论子类

一般评论实体

/**
* @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() 这样的父类(super class)方法将自动插入到 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 的代码生成:generate:entities我的

我知道有很多关于这个主题的帖子,但没有任何帮助解决我的问题。我做了:

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

有人可以指出我的问题或知道可能导致此问题的原因吗?我因为这个恶心的问题浪费了一整天的时间。

最佳答案

好吧,首先你必须明白,将 $id 设置为 protected 是唯一正确的解决方案。仅此而已。

Doctrine 在所有子类中生成重复的私有(private)$id,这是事实。我在 mongodb 项目中遇到了同样的问题。据我了解,这是教义错误,你对此没有任何关系。

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

关于symfony - Doctrine2迁移-单表继承-父id不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21633821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com