Doctrine2 ORM不刷新在脚本之外更改的对象 [英] Doctrine2 ORM doesn't refresh objects that are altered outside of a script

查看:99
本文介绍了Doctrine2 ORM不刷新在脚本之外更改的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道如何标题或搜索已经发布的问题,所以我很抱歉,如果这在之前见过。

I really didn't know how to title this or search for an already posted question, so my apologies if this has been seen on here before.

我是使用以下代码获取一些不期望的结果:

I am getting some undesired results with the following code:

// get object managers
$fooManager = $this->getContainer()->get('foo_manager');
$barManager = $this->getContainer()->get('bar_manager');

// infinite loop
for (;;) {

    // keep getting unitialized "foo" objects, or quit if none
    while (($foo = $fooManager->findUninitialized()) !== null) {

        // an uninitialized "foo" will need to make a "bar" object
        $bar = $barManager->create();
        $bar->setA('...');

        // save "bar" to database, update() WILL perform a flush()
        $barManager->update($bar);

        // make the association of "bar" to "foo"
        $foo->setBar($bar);

        // save "foo" to database, update() WILL perform a flush()
        $fooManager->update($foo);

    }

    // keep getting unprocessed "foo" objects, or quit if none
    while (($foo = $fooManager->findUnprocessed()) !== null) {

        // process the data from "foo" object's "bar"
        process($foo->getBar()->getB());

    }

}

你可以看到在第一个 while 循环中, $ bar 对象正在建立并放入数据库。另一个脚本正在捡拾这些东西,并向他们做事情。

You can see that in the first while loop, $bar objects are being made and put into the database. Another script is picking up on these and doing things to them.

在第二个 while 循环中,code> $ foo 对象正在尝试访问其修改的bar对象(注意调用的 getB(),我们可以假设在另一个脚本中单独执行 setB()用于更改对象的状态)。

In the second while loop, the $foo objects are trying to access their modified "bar" object (notice the getB() being called, we can assume that in another script being executed separately setB() was used to change the state of the object).

但是,在第二个循环中,当我调用 getB()时,它不返回使用 setB()在其他脚本中。

However, in the second loop, when I call getB(), it is not returning the value I set with setB() in that other script.

我可以确保所有的数据都被正确地持久化(并被刷新)到数据库中,所以当 getB )在该第二个循环中被调用,无论是持有B被更改并存在于数据库中。

I can ensure all of the data is being properly persisted (and flushed) to the database, so when the getB() is called in that second loop, whatever was holding "B" was changed and is present in the database.

有一件事注意到,在日志文件中,当我在第二个调用 $ foo-> getBar()时,我没有看到提取bar的数据的查询循环(这些应该是懒惰加载)。另外,如果我在第二个循环中从 $ foo> getBar()返回的值 var_dump()它看起来像相关联的bar对象将在第一个循环结束时看起来。

One thing I've noticed is that in the log file, I am not seeing a query to pull in the data for "bar" when I call $foo->getBar() in the second loop (these should be lazily loaded). Also, if I var_dump() the value returned from $foo->getBar() in the second loop, it looks exactly like the associated "bar" object would look by the end of the first loop.

我在想,有某种缓存或某些事情发生(因为我在脚本执行之前更改了这些bar对象,所以Doctrine似乎只是重新使用这些对象而不是从数据库重新加载数据)。

I'm thinking that there is some kind of caching or something going on (since I've altered those "bar" objects earlier in the script execution, Doctrine seems like it is just re-using those rather than reloading their data from the database).

我知道我想做的是可能不常见。我正在做的是使用Symfony2控制台命令功能来编写一些命令行守护程序来帮助处理一些数据;包含此代码的守护进程与另一个守护进程一起运行,处理那些创建的bar对象。

I know what I am trying to do is probably not common. What I am doing is using the Symfony2 console command features to write a few command-line "daemons" to help with processing some data; the "daemon" that contains this code is being run in conjunction with another "daemon" that processes those "bar" objects that get created.

对于是否有当我调用 $ foo> getBar()方法时,将其修复到第二个循环中的一个方法,我实际上是从数据库中提取更新的数据。非常感谢任何帮助。

Curious as to whether there is a way to fix this to where in my second loop when I call the $foo->getBar() method, I am actually pulling the updated data from the database. Any help is greatly appreciated.

推荐答案

这是真正由用户提供的 chocoDeveloper 。在实体管理器上使用 refresh($ entity)方法解决了我的问题。

This was really provided by the user ChocoDeveloper. Making use of the refresh($entity) method on the entity manager solved my problem.

例如: / p>

For example:

$em->refresh($entity);

这篇关于Doctrine2 ORM不刷新在脚本之外更改的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆