Can I create a new Document Object and then keep persisting over and over with the document manager in Doctrine within the same linear list of instructions?
I am implementing a logging system within my Symfony2 Project with Doctrine, Stripe and MongoDB. Basically I want to keep logging progress as each instruction is executed. The reason for this is because sometimes the instructions can only reach a certain point because the Bank will decline the card and an exception will be thrown. There are no 100% definite errors. And I want to be able to pick up where I left off.
For instance.
$charge = new StripeChargeCardObject(); //this is the Document
$charge->setEvent("card.charger");
$charge->setPhase(1);
$this->dm = new DocumentManager();
$dm->persist($charge);
$dm->flush();
$payload = new StripePayload($event);
$charge->setPhase(2);
$charge->setPayload($payload);
$this->dm->persist($charge);
$this->dm->flush();
.....
As I keep going down the list, changing the values of the Document until there is an Exception thrown.
Can I use the same $charge Document, keep editing it, and keep persisting it? Or do I need to load it again each and every time after I persist and flush?
答案 0 :(得分:1)
只要您不刷新,数据库中就不会保存任何内容,因此您仍然可以编辑对象并保持不变。
在实际代码中,每次读取指令时都会创建一个新对象。我不确定这是你想要的行为。
如果你想在DB中有一个对象,你应该获取你想要编辑的对象(并且只有当你的查询返回null结果时才创建一个新对象。)
希望它能帮助你看到对你有用的东西!