0
我使用學說2.1 我有相同的記錄兩個對象,一個堅持,一個不持久。我不想重寫所有的屬性,而是希望將ID分配給新的屬性,並調用persist()以便更新正確的行。在僞碼的自舉看起來是這樣的:原則:如何更新對象
$old_a = new A(name: "a", value: "old")
$em->persist($old_a);
$em->flush()
現在我已經在數據庫中的行名稱爲「A」和值「老」。我想有價值「新」。我可以做
$a = new A(name: "a", value: "new") // create new object
$query = $em->createQuery("SELECT A a WHERE name = 'a'") // check if object with the same name already exists
$old_a = $query->getSingleResult();
$old_a->setValue($a->getValue()) // update value with the new one - here is the problem! If there are many properties I do not want to invoke many times setXXX($a->getXXX). I would like to do something like $old_a = $a or $a->setId($old_a->getId())
$em->persist(old_a) // update the row
究竟是什麼你想怎麼辦?示例中的兩個實體在持久化之前都不在數據庫中。你是否從數據庫中獲得了$ old_a實體,並希望從不受Doctrine管理的其他實體更新其某些字段? – gilden
@gilden請善待第一個代碼示例爲「引導」 - 我會更新我的問題,以使其更清晰 – mkk
你想使一個副本,如果對象和改變的值,並堅持它作爲一個新的? –