Symfony 2 - 基于另一个表中新插入的记录更新表

问题描述:

我试图创建一个使用Symfony 2和Doctrine 2的小型论坛应用程序。我的ForumTopic实体有一个last_post字段(oneToOne映射)。现在当我坚持我的新职位Symfony 2 - 基于另一个表中新插入的记录更新表

$em->persist($post); 

我想更新我的ForumTopic实体,所以它的last_post字段将引用这个新的职位。我刚刚意识到,它不能与教义postPersist监听器来完成,所以我决定用一个小黑客,并尝试:

$em->persist($post); 
$em->flush(); 
$topic->setLastPost($post); 
$em->persist($post); 
$em->flush(); 

,但它似乎没有更新我的主题表。

我也看看http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/working-with-associations.html#transitive-persistence-cascade-operations希望通过在我的Topic.orm.yml文件中添加级联:['persist']来解决问题,但它也没有帮助。

任何人都可以指向我的解决方案或示例类吗?

我ForumTopic是:

FrontBundle\Entity\ForumTopic: 
     type: entity 
     table: forum_topics 
     id: 
       id: 
         type: integer 
         generator: 
           strategy: AUTO 
     fields: 
       title: 
         type: string(100) 
         nullable: false 
       slug: 
         type: string(100) 
         nullable: false 
       created_at: 
         type: datetime 
         nullable: false 
       updated_at: 
         type: datetime 
         nullable: true 
       update_reason: 
         type: text 
         nullable: true 
     oneToMany: 
       posts: 
         targetEntity: ForumPost 
         mappedBy: topic 
     manyToOne: 
       created_by: 
         targetEntity: User 
         inversedBy: articles 
         nullable: false 
       updated_by: 
         targetEntity: User 
         nullable: true 
         default: null 
       topic_group: 
         targetEntity: ForumTopicGroup 
         inversedBy: topics 
         nullable: false 
     oneToOne: 
       last_post: 
         targetEntity: ForumPost 
         nullable: true 
         default: null 
         cascade: [ persist ] 
     uniqueConstraint: 
       uniqueSlugByGroup: 
         columns: [ topic_group, slug ] 

而且我ForumPost是:

FrontBundle\Entity\ForumPost: 
     type: entity 
     table: forum_posts 
     id: 
       id: 
         type: integer 
         generator: 
           strategy: AUTO 
     fields: 
       created_at: 
         type: datetime 
         nullable: false 
       updated_at: 
         type: datetime 
         nullable: true 
       update_reason: 
         type: string 
         nullable: true 
       text: 
         type: text 
         nullable: false 
     manyToOne: 
       created_by: 
         targetEntity: User 
         inversedBy: forum_posts 
         nullable: false 
       updated_by: 
         targetEntity: User 
         nullable: true 
         default: null 
       topic: 
         targetEntity: ForumTopic 
         inversedBy: posts 
+0

有趣的是:在我将映射信息转换为注释之后,它的功能就像魅力一样... – GergelyPolonkai 2012-07-13 11:55:32

我相信你正在做自己该更加困难,因为你认为你必须刷新你的帖子然后才能将其设置为关于您的主题的关联。

学说足够聪明,如果你坚持一个实体并在没有先调用flush的情况下将它设置为一个关联,它将确保当你调用flush时,它首先坚持该实体,以便它具有一个ID可以与该关联一起使用。

这意味着什么是你真正需要做的是这样的:

// Assume that topic has been fetched from the DB, and post is completely new 
$topic = $em->find('TopicModel', $topicId); 
$post = new ForumPost(); 
// Fill in the post info 
// Set up the association 
$topic->setLastPost($post); 
// And finally, persist and flush - no more effort needed 
$em->persist($post); 
$em->flush(); 

这方面的一个很好的副作用是,你可以简单地使用一个prePersist事件侦听器来更新线程的最后一个职位 - 学说会照顾你的一切。另外,如果你想要一个可以更容易地遵循逻辑的方法,你可以让你的Post模型自己调用setLastPost这个主题 - 例如,如果你在构造函数或setTopic中设置了帖子的主题()方法,在那里添加setLastPost调用。

这样做是相对常见的做法,让协会的一方照顾双方,以保持良好的同步 - 请参阅Working with Associations

+0

我试过了后一种方法,但Doctrine似乎没有在forum_topics表上做更新。我甚至试图调用$ em-> persist($ topic),但它是一样的,没有效果。 – GergelyPolonkai 2012-07-08 22:52:46

+0

嗯,有趣 - 有三种情况,我知道这可能导致: 1.主题实体不由EntityManager管理,因此它无法检测到更改(如果您从数据库中获取主题,它不太可能是这样的) 2.该关联没有被正确映射(仅对关联的拥有一侧进行更改) 3。主题实体配置了明确的或通知的变更跟踪 您是否可以使用两种模型的映射更新您的问题以帮助诊断? – 2012-07-09 05:37:08