Php 条令实体关系不更新

Php 条令实体关系不更新,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我正在尝试更新实体的多对一关系对象 这是我正在使用的代码: $em = $this->getDoctrine()->getManager(); $request = Request::createFromGlobals(); $user = $this->get('security.context')->getToken()->getUser(); # find thread $thread = $em->getRepository('MyBun

我正在尝试更新实体的多对一关系对象

这是我正在使用的代码:

$em    = $this->getDoctrine()->getManager();
$request = Request::createFromGlobals();
$user    = $this->get('security.context')->getToken()->getUser();

# find thread
$thread = $em->getRepository('MyBundle:Thread')->findThreadById($threadid);

# find thread posts
$query = $em->getRepository('MyBundle:Post')->findThreadPosts($thread->getId());

# create form
$form = $this->createForm(new PostType(), new Post($user, $thread));

# handle form
$form->handleRequest($request);
if ($form->isValid()) {

    $post = $form->getData();
    $em->persist($post);
    $em->flush();
    $logger->debug("POST CREATED with id: " . $post->getId());

    $thread->setLastPost($post);
    $thread->addPostCount(1);
    $thread->setLastPostCreator($user);
    $em->flush();


    $logger->debug("ADDED LAST POST TO thread with id: " . $thread->getId());

    $forum = $thread->getForum();
    $forum->setLastPostCreator($user);
    $forum->addPostCount(1);
    $forum->setLastPostCreator($user);
    $forum->setLastPost($post);
    $em->flush();

    $this->get('session')->getFlashBag()->add(
        'notice',
        'Your post has been added!'
    );
}

$data['posts']  = $posts;
$data['thread'] = $thread;
$data['form']   = $form->createView();
测绘站:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="MyBundle\Entity\Post" table="post">

    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="text" type="text" column="text" nullable="false"/>

    <many-to-one field="thread" target-entity="Thread" inversed-by="posts">
      <join-columns>
          <join-column name="thread_id" referenced-column-name="id"/>
      </join-columns>
    </many-to-one>

  </entity>
</doctrine-mapping>
因此,每当我添加帖子时,我都会更新它的父线程。因此,该线程具有多对一引用的最新帖子。我还更新了计数,就像缓存和减少连接一样

计数确实正确地增加了。但是,最新发布的参考资料未更新。当我查看调试日志时,我还看到没有为此执行任何查询


对此有什么想法/想法吗?我最好的猜测是它没有检测到更改,但我不知道如何使此类数据无效。

您的$thread->setLastPost($post)方法是否如下所示:

public function setLastPost(Post $post)
{
    $this->lastPost = $post;
}
有人打电话吗?插入调试语句时会发生什么

如果需要,您是否按参考设置了正确的位置

"So, all that by_reference=false really does is force the framework to call the setter on the parent object."


如果上述建议不起作用,请您提供更多信息,如表单类型和相关的实体设置和获取者

您的$thread->setLastPost($post)方法是否如下所示:

public function setLastPost(Post $post)
{
    $this->lastPost = $post;
}
有人打电话吗?插入调试语句时会发生什么

如果需要,您是否按参考设置了正确的位置

"So, all that by_reference=false really does is force the framework to call the setter on the parent object."


如果上述建议不起作用,请您提供更多信息,如表单类型和相关的实体设置和获取者

我认为问题在于你正在建立并坚持这种关系的“反面”。文件说:

条令只会检查关联的拥有方是否有变更

manytone始终是双向关联的拥有方

因此,在您的例子中,线程和lastPost之间有一个多通双向关系,其中lastPost实体是“拥有方”(多通方),线程是“反向方”。我认为应该在Post实体上有一个setThread()函数,如果在处理表单后运行$Post->setThread($thread),然后再运行persist($Post),事情就会解决


此外,在我看来,您应该在thread->posts之间建立一个关系,并使用一个自定义存储库函数,或者根据createdAt日期(假设它存在)进行查找和排序,以获得“last post”。在我看来,多个线程到最后一个帖子的关系并不正确,一个线程不应该只有一个“最后一个帖子”(也称为oneToOne关系)吗?

我认为问题在于你正在设置并坚持关系的“反面”。文件说:

条令只会检查关联的拥有方是否有变更

曼尼通始终是双向关联的拥有方

因此,在您的例子中,线程和lastPost之间有一个多通双向关系,其中lastPost实体是“拥有方”(多通方),线程是“反向方”。我认为应该在Post实体上有一个setThread()函数,如果在处理表单后运行$Post->setThread($thread),然后再运行persist($Post),事情就会解决



此外,在我看来,您应该在thread->posts之间建立一个关系,并使用一个自定义存储库函数,或者根据createdAt日期(假设它存在)进行查找和排序,以获得“last post”。在我看来,多个线程到最后一篇文章的关系并不正确,一个线程不应该只有一个“最后一篇文章”(也称为oneToOne关系)吗?

我刚刚遇到了这个问题。我的解决方案是调用
$em->merge($entity)
位于“多”侧的每个实体上,后跟
$em->flush()

我刚刚遇到了这个。我的解决方案是调用
$em->merge($entity)
位于“多”侧的每个实体上,后跟
$em->flush()

我们需要查看帖子和线程的映射。使用mappingsIs
Thread::lastPost更新了我的原始帖子
设置从不?还是在第一次设置关联后无法更改?你能用
线程
创建来添加操作吗?它正在被设置,但只是没有被持久化到数据库中。我们需要查看Post和线程的映射。用mappingsIs
线程::lastPost
设置never更新了我的原始帖子?还是在第一次设置关联后无法更改?你能用
线程
创建来添加动作吗?它正在设置中,但没有被持久化到数据库中。你好,谢谢你的回复。将调用setLastPost函数,该函数与您发布的setter类似。由于我获取线程,因此不需要by_引用。我用更详细的代码更新了我原来的帖子。我认为表单类型与本例无关。我还添加了我的实体执行此操作时发生的情况:公共函数setLastPost(Post$Post){$this->lastPost=$Post;$Post->setThread($this);}没有任何更改,Post已经与线程关联。正如这里定义的:
newpost($user,$thread)
Hello,感谢您的回复。将调用setLastPost函数,该函数与您发布的setter类似。由于我获取线程,因此不需要by_引用。我用更详细的代码更新了我原来的帖子。我认为表单类型与本例无关。我还添加了我的实体执行此操作时发生的情况:公共函数setLastPost(Post$Post){$this->lastPost=$Post;$Post->setThread($this);}没有任何更改,Post已经与线程关联。如此处所定义:
newpost($user$thread)
最后一篇文章是一个沉重的查询,它也经常在繁忙的论坛上更新。这就是为什么我决定有一个缓存字段(通常在RDBMS中从未使用过)如果我错了,请纠正我。我正在测试你的解决方案。是的,没错,应该是一对一的。现在调整,如果工作尽快,将会更新,这是有意义的。我加了一张便条