1

I got error in line 167 it told me The EntityManager is closed? I used symfony2 and also aurelia. I put "processUserDetails" for going through it.

 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);
    }

enter image description here

I got error in line 167 it told me The EntityManager is closed? I used symfony2 And aurelia as well , I put "processUserDetails" for going through it.

COULD ANY ONEE HELP ME?

Tima mehro
  • 73
  • 1
  • 8
  • I guess EntityManager is responsible for connecting to database and do some stuff with it. And you were finishing the function call and terminate the connection too the database too early. Is there anything async in your block of code above that you need to await for? – bigopon Sep 18 '18 at 10:05
  • @bigopon ,No ,Could you check fluch () in processuser detail ?I think problem is it but I have no idea about correct place of it – Tima mehro Sep 18 '18 at 19:37
  • Oh yes, that should be the issue. your flush() caused the connection to close. that's why the error. you should only put it at the end. of where it comes from. – bigopon Sep 18 '18 at 19:57
  • @bigopon which one is not correct?I didnot get where should I put fluch? – Tima mehro Sep 18 '18 at 22:27

1 Answers1

1

You are flushing too early in your processUserDetails. It requires refactoring of the method / class to make sure flush() is called at appropriate time. One quick fix is to let the method accept EntityManager as parameter, and an optional parameter to tell it not to flush at the end:

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();
    }
}

Then inside your 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;

Note that doing this changes how your processUserDetails works, you should re validate the flow. The key point is to hold every entity in memory and only call flush() once at the end.

bigopon
  • 1,924
  • 2
  • 14
  • 23