php 来自实体的 Doctrine 2 更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5646026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:04:58  来源:igfitidea点击:

Doctrine 2 update from entity

phpormdoctrine-orm

提问by Dennis

Is it possible to update an entity in a similar way as below:

是否可以以如下类似的方式更新实体:

$data       = new ATest();  // my entity
$data->id   = 1;            // id 1 already exists, I just want to update this row
$data->name = "ORM Tested"; // changed the name

$entityManager->persist($data);
$entityManager->flush();

This will insert and change the id of the object instead of updating the existing row in the database.

这将插入和更改对象的 id,而不是更新数据库中的现有行。

回答by Francesco Casula

You should call merge instead of persist:

你应该调用merge而不是persist:

$data = new MyEntity();
$data->setId(123);
$data->setName('test');

$entityManager->merge($data);
$entityManager->flush();

回答by Dennis

I had to use

我不得不使用

$entityManager->merge($data)

回答by Tony Bogdanov

Or just get the managed entity rather than an empty one.

或者只是获取托管实体而不是空实体。

$data = $entityManager->getRepository('ATest')->findOne(1); // ATest is my entitity class
$data->name = "ORM Tested"; // just change the name

$entityManager->persist($data);
$entityManager->flush();

If the entity is already managed, persist() will update it rather than insert a new one.

如果实体已经被管理,persist() 将更新它而不是插入一个新的。

回答by fyrye

You can also use getReferenceto update an entity property by identifier without retrieving the database state.

您还可以使用getReference标识符来更新实体属性,而无需检索数据库状态。

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/advanced-configuration.html#reference-proxies

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/advanced-configuration.html#reference-proxies

This will establish a simple Proxy to work with the Entity by ID instead of instantiating a new Entityor explicitly getting the Entity from the database using find(), which can then be updated by flush.

这将建立一个简单的代理以通过 ID 与实体一起工作,而不是使用 实例化一个new Entity或从数据库中显式获取实体find(),然后可以通过刷新进行更新。

$data = $entityManager->getReference('ATest', $id);
$data->setName('ORM Tested');
$entityManager->flush();

This is especially useful for updating the OneToManyor ManyToManyassociations of an entity. EG: $case->addTest($data);

这是用于更新特别有用OneToManyManyToMany实体的关联。例如:$case->addTest($data);

It is generally bad practice to manually set the identifier of a new Entity, even if the intent is to update the entity. Instead it is usually best to let the EntityManager or Entity constructor establish the appropriate identifiers, such as a UUID. For this reason Doctrine will generate entities by default with the identifier as a private property with no setter method.

手动设置新实体的标识符通常是不好的做法,即使目的是更新实体。相反,通常最好让 EntityManager 或 Entity 构造函数建立适当的标识符,例如UUID. 出于这个原因,Doctrine 将默认生成带有标识符的实体,作为没有 setter 方法的私有属性。