小编Val*_*yev的帖子

如何在Doctrine 2中丢弃对实体的非刷新更改?

我长期使用基于SQL查询的数据库访问级别.但现在我决定使用ORM Doctrine2.此刻,我对实体工作流程存在一个概念上的误解.

最好通过示例描述:

 try {
   $user = $entityManager->find('User', 1);
   $user->setName('New name');
   $entityManager->flush();
 }
 catch (...)
 {
   // !we are here: changes failed and $user-row in DB was not updated
 }

 // ...

 $user = $entityManager->find('User', 1);
 $user->setOther('Other');
 $entityManager->flush(); // <- in this request both [other] and [name] fields will be updated to DB, but only [other] update expected
Run Code Online (Sandbox Code Playgroud)

在第一个代码块中,我获取了$ user对象.更改[名称]并尝试保存,但失败了.在第二个块(与第一个块无关)中,我再次获取了$ user对象.但是ORM第一次返回了与同一对象的链接.所以这个对象已经改变了[name]属性.在第二个块的最后一行,我只想保存[其他]字段,但[other]和[name]都会更新.

解决这种情况的正确方法是什么?

orm doctrine doctrine-orm

9
推荐指数
2
解决办法
6929
查看次数

标签 统计

doctrine ×1

doctrine-orm ×1

orm ×1