2017-04-12 104 views
0

我們正在爲我們的項目使用Symfony,並且還有一些關於Doctrine的內容,我無法跟上。 Doctrine的實體管理器(以下稱它爲'em')是一項共享服務,因此當我將它們注入多個服務時,它們共享完全相同的em實例。這是簡單的。如果我介紹一個例子就說明我要問:請看下面的例子:Shared Doctrine EntityManager服務

$service1 = $this->get('vendor_test.service_one'); // $service1 has a private entity manager property 
$service2 = $this->get('vendor_test.service_two'); // $service2 as well has a private entity manager property 

$entity1 = $service1->getEntityById(1); // getEntityById() queries for an entity with the given id and returns it. So it is in the managed list of service1's entity manager 
$entity2 = $service2->getEntityById(2); // entity1 and entity2 not necessarily of the same class 

$entity1 
    ->setProperty1('aaaa') 
    ->setProperty2($service2->updateDateTime($entity2)) // updateDateTime() let's say updates a datetime field of the passed entity (in this case entity2) and calls $this->entityManager->flush(); and returns the datetime. 
    ->setProperty3('bbbb') 

$service1->save(); // calls $this->entityManager->flush() so it should update the managed entities (in this case entity1) 

所以現在的問題是:如果服務1和服務2的EntityManager對象是EntityManager的同一實例,以便他們是相同的,他們共享相同的內部管理列表,然後當調用$service2->updateDateTime($entity2)做一個entityManager->flush()時,它是否也刷新$ entity1?具有Property1設置爲'aaaa'的$ entity1在中途刷新並在數據庫中更新,並在$ service1-> save()時被刷新第二步。叫做?

希望我設法弄清了我的意思和我想問的問題。

回答

0

當我測試了一下,並問一個更能幹的人,答案是肯定的,因爲我到處都使用實體經理,他們都共享相同的託管列表。爲了解決問題中提到的問題,是將實體傳遞給實體管理器,其他實體將保持完好。

相關問題