第167行出现错误,它告诉我EntityManager已关闭? 我使用了symfony2和aurelia。 我把“ processUserDetails”通过它。
private function processUserDetails($library, $entity, $oldDetails, $settings) {
$newDetails = $entity->getDetails();
$em = $this->getDoctrine()->getManager();
$this->disableBlameableEventListeners($em);
foreach($oldDetails as $detail) {
if(!$newDetails->contains($detail)) {
$library->removeResource($detail->getResource());
$em->remove($detail);
}
}
$em->flush();
$this->enableBlameableEventListeners($em);
foreach($newDetails as $detail)
if(!$detail->getId()) $em->persist($detail);
$em->flush();
foreach($newDetails as $detail) {
$resource = $detail->getResource();
if(!$library->getResources()->contains($resource))
$library->addResource($resource);
}
$em->flush();
if($this->getUser() === $entity->getUser()) {
$this->putMembershipContactSetting($entity, $settings);
$locale = $entity->getLocale();
if($locale && $entity->getUser()->getLocale() !== $locale)
$this->setUserLocale($entity);
$em->flush();
}
}
public function putMembershipAction(Request $request, Company $company, Membership $entity) {
if(!$this->canTouchIt($company, 'RolesManage') && $this->getUser() !== $entity->getUser())
return FOSView::create('Unauthorized', Codes::HTTP_UNAUTHORIZED);
$em = $this->getDoctrine()->getManager();
$settings = $request->request->get('contact_settings');
$request->setMethod('PATCH');
$library = $entity->getAssets();
$form = $this->createForm(
new MembershipType($this->isAdminUser()), $entity,
array('method' => $request->getMethod())
);
$oldDetails = $entity->getDetails();
$entity->setDetails(new ArrayCollection);
$this->removeExtraFields($request, $form);
$form->handleRequest($request);
if($form->isValid()) {
if($entity->getPermissions())
$entity->getPermissions()->setMembership($entity);
if($entity->getStatus() === Membership::STATUS_ACCEPTED)
$this->processUserDetails($library, $entity, $oldDetails, $settings);
$this->get('desygner_brand.membership.manager')
->addMemberToGlobalGroup($entity);
$serializer = $this->get('jms_serializer');
$context = SerializationContext::create()->setGroups(array('membership'));
$this->get('monolog.logger.behaviour')->error('Company log #'.$company->getId(), array(
'type' => 'Modify memeber',
'user' => $this->getUser()->getId(),
'data' => json_decode($this->get('jms_serializer')
->serialize($entity, 'json', $context), true
),
));
return $entity;
}
return FOSView::create(array('errors' => $form->getErrors()), Codes::HTTP_PRECONDITION_FAILED);
}
第167行出现错误,它告诉我EntityManager已关闭? 我也使用了symfony2和aurelia,并在其中放置了“ processUserDetails”。
有人可以帮助我吗?
答案 0 :(得分:1)
您在processUserDetails
中冲洗得太早了。它需要重构方法/类,以确保在适当的时候调用flush()
。一种快速解决方法是让该方法接受EntityManager
作为参数,并使用一个可选参数告诉该方法最后不要刷新:
private function processUserDetails($em, $library, $entity, $oldDetails, $settings, $doNotFlush) {
$newDetails = $entity->getDetails();
$this->disableBlameableEventListeners($em);
foreach($oldDetails as $detail) {
if(!$newDetails->contains($detail)) {
$library->removeResource($detail->getResource());
$em->remove($detail);
}
}
// flushing too early ?
// $em->flush();
$this->enableBlameableEventListeners($em);
foreach($newDetails as $detail)
if(!$detail->getId()) $em->persist($detail);
// flushing too early ?
// $em->flush();
foreach($newDetails as $detail) {
$resource = $detail->getResource();
if(!$library->getResources()->contains($resource))
$library->addResource($resource);
}
// flushing too early ?
// $em->flush();
if($this->getUser() === $entity->getUser()) {
$this->putMembershipContactSetting($entity, $settings);
$locale = $entity->getLocale();
if($locale && $entity->getUser()->getLocale() !== $locale)
$this->setUserLocale($entity);
// flushing too early ?
$em->flush();
}
if (!doNotFlush) {
$em->flush();
}
}
然后在您的putMembershipAction()
中:
if($entity->getStatus() === Membership::STATUS_ACCEPTED)
$this->processUserDetails($em, $library, $entity, $oldDetails, $settings, true);
// ....
// before return, flush it
$em->flush();
// now returns
return $entity;
请注意,这样做会改变processUserDetails
的工作方式,您应该重新验证流程。关键是将每个实体保存在内存中,最后只调用一次flush()
。