我没有在我的博客中获得简单的表继承。
这是我所拥有的:
3个简单的课程
- 一般摘要注释类
- 子类,用于对具有外键的博客文章进行评论
- 子类,用于注释活动也带有外键
一般性评论实体
/**
* @ORMMappedSuperClass
* @ORMEntity(repositoryClass="MYBlogBundleEntityRepositoryCommentRepository")
* @ORMTable(name="blog_comment")
* @ORMInheritanceType("SINGLE_TABLE")
* @ORMDiscriminatorColumn(name="type", type="string")
* @ORMDiscriminatorMap( {"blogentry" = "BlogComment", "activity" = "ActivityComment"} )
* @ORMHasLifecycleCallbacks
*/
abstract class Comment
{
/**
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMColumn(name="title", type="string", length=255, nullable=true)
* @AssertNotBlank()
*/
private $title;
...
博客评论
/**
* BlogEntryComment
* @ORMEntity(repositoryClass="MYBlogBundleEntityRepositoryBlogEntryCommentRepository")
*/
class BlogComment extends Comment
{
/**
* @ORMManyToOne(targetEntity="MYBlogBundleEntityBlogEntry", inversedBy="comments")
* @ORMJoinColumn(name="blog_entry_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*/
private $blogEntry;
...
活动注释
/**
* @ORMEntity(repositoryClass="MYBlogBundleEntityRepositoryActivityCommentRepository")
*/
class ActivityComment extends Comment
{
/**
* @ORMManyToOne(targetEntity="MYBlogBundleEntityActivity", inversedBy="comments")
* @ORMJoinColumn(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 MYBlogBundleEntityActivityComment::$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
DoctrineCommonPersistenceMappingRuntimeReflectionService->getAccessibleProperty() at MY_PATH/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php:889
DoctrineORMMappingClassMetadataInfo->wakeupReflection() at MY_PATH/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:537
DoctrineORMMappingClassMetadataFactory->wakeupReflection() at MY_PATH/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:209
DoctrineCommonPersistenceMappingAbstractClassMetadataFactory->getMetadataFor() at MY_PATH/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:114
DoctrineCommonPersistenceMappingAbstractClassMetadataFactory->getAllMetadata() at MY_PATH/vendor/doctrine/migrations/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/DiffCommand.php:68
DoctrineDBALMigrationsToolsConsoleCommandDiffCommand->execute() at MY_PATH/vendor/doctrine/doctrine-migrations-bundle/Doctrine/Bundle/MigrationsBundle/Command/MigrationsDiffDoctrineCommand.php:49
DoctrineBundleMigrationsBundleCommandMigrationsDiffDoctrineCommand->execute() at MY_PATH/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:252
SymfonyComponentConsoleCommandCommand->run() at MY_PATH/vendor/symfony/console/Symfony/Component/Console/Application.php:900
SymfonyComponentConsoleApplication->doRunCommand() at MY_PATH/vendor/symfony/console/Symfony/Component/Console/Application.php:192
SymfonyComponentConsoleApplication->doRun() at MY_PATH/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:96
SymfonyBundleFrameworkBundleConsoleApplication->doRun() at MY_PATH/vendor/symfony/console/Symfony/Component/Console/Application.php:123
SymfonyComponentConsoleApplication->run() at MY_PATH/app/console:22
由于某些原因,它找不到子类的 id 属性。
更改访问级别会导致错误的代码创建。如果设置为$id
public
或protected
将使用任务./app/console doctrine:generate:entities MY
中断代码生成
我知道有很多关于这个主题的帖子,但没有任何帮助来解决我的问题。我做了:
- 禁用所有缓存
- 每次都清除缓存,甚至删除应用程序/缓存
- 删除所有文件并从头开始
- 包含在每个子类中
@ORMTable(name="blog_comment")
- 尝试了不同的访问级别修改
有人可以指出我的问题或知道可能导致这种情况的原因。在这个令人作呕的问题上,我浪费了整整一天的工作。
好的,首先您必须了解,将$id设置为protected
是唯一正确的解决方案。就这样。
教义在所有子类中产生重复的私人$id,这是真的。我在我的教义mongodb项目中遇到了同样的问题。据我了解,这是教义错误,你与此无关。
只能在每次命令运行后删除 doctrine 在子类中生成的所有额外字段和generate:entities
方法。