0
votes

Almost same ISSUES: link, link

UPDATE INFO: - $user = $this->getUser(); set the old image while edit(error form submit). The image replaced with the submitted one value(only value not displaying). WHILE ERROR FORM SUBMIT - I NEED TO DISPLAY THE OLD MEDIA.

NO RELATION WITH SONATA ADMIN.

I have Admin and User role. Both have seperate admin area. User admin area has more complex structure.

I added an Image(avatar) to SonataUser , it works good at admin. Its OneToOne - User and Media.

To edit profile at User Dashboard( Its not SonataAdmin - its i created seperately, Its a simple symfony style).

code:

public function editProfileAction() {
        $user = $this->getUser();
        if (!is_object($user) || !$user instanceof UserInterface) {
            throw $this->createAccessDeniedException('This user does not have access to this section.');
        }
        // Check user has allready media?
        $om = $this->getUser()->getImage();
        $oldPath = $om ? $this->getMediaPath($om->getId()) : NULL;
        $form = $this->creteForm();
        $formHandler = $this->get('sonata.user.profile.form.handler');
        $process = $formHandler->process($user);
        if ($process) {
            // if new file - delete old file
            $this->deleteOldMedia($om, $oldPath);
            $this->flashMSG(0, 'Profile updated!');
            return $this->redirectToRoute('fz_user');
        }
        $x = ['cmf' => '', 'pTitle' => 'Profile'];
        return $this->render(self::TEMPLATE, ['x' => $x, 'form' => $form->createView()]);
    }

By the above code, works - with one problem. The reference image of old file is not deleting at server folder. New files are added and entity works fine (displaying at template - fine).

So I tried with my own code,

public function editProfileAction() {
        $request = $this->get('request');
        $user = $this->getUser();
        if (!is_object($user) || !$user instanceof UserInterface) {
            throw $this->createAccessDeniedException('This user does not have access to this section.');
        }
        // Check user has allready media?
        $om = $this->getUser()->getImage();
        $oldPath = $om ? $this->getMediaPath($om->getId(), 'reference') : NULL;
        $oldTN = $om ? $this->getMediaPath($om->getId(), 'admin') : NULL;
        $form = $this->createForm(ProfileType::class, $user);
        $form->handleRequest($request);
        $em = $this->getDoctrine()->getEntityManager();
        $data = $form->getData();
        if ($form->isSubmitted() && $form->isValid()) {
            if (($oldPath != NULL) && ($data->getImage()->getBinaryContent() != NULL)) {
                $this->deleteFile($oldPath);
                $this->deleteFile($oldTN);
            }
            $em->persist($user);
            $em->flush();
            $this->flashMSG(0, 'Profile updated!');
            return $this->redirectToRoute('fz_user');
        }
//        $$user->setImage($om);
        $x = ['cmf' => '', 'pTitle' => 'Profile'];
        return $this->render(self::TEMPLATE, ['x' => $x, 'form' => $form->createView()]);
    }

My own code works - with one problem, If image validation is error - the all image at the template are disappeared. So to check i added $user->setImage(NULL); , the result is, the Null image is shown.(NULL image means at template i do if(null){ display my image }). The backend process - image upadate works good.

For now - I'm satisfied with my code. Here I need to make $user->setImage(xx); to the real image. while form submit with error on media. ONly at error on media.

If no media and error submit - works (displaying image).

UPDATE: I used $em->refresh($user); from this answer , also it failed to update my image.

WHAT I FOUND ISSUE WITH USER: Its not using 'ApplicationSonataUserBundle:User' for SYMFONY app.user . Thats why, when i give $em->refresh($user); it not modifing username and other details. But it modifing the new details of ApplicationSonataUserBundle:User

1
Here i'm handling the current user info. Not fetched from DB. Do i'm working correctly? - Sudhakar Krishnan

1 Answers

0
votes

Finally to solve I REDIRECTED with flash msg..

$em = $this->getDoctrine()->getManager();
        $user = $this->get('security.token_storage')->getToken()->getUser();
        $entity = $em->getRepository('ApplicationSonataUserBundle:User')->find($user->getId());
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity.');
        }
        $form = $this->createForm(ProfileType::class, $entity);
        if ($request->getMethod() === 'POST') {
            $form->handleRequest($request);
            if ($form->isValid()) {
                $em->flush();
                return $this->redirectToRoute('fz_user');
            }
            $em->refresh($user);
            $this->flashMSG(1, '' . $form->getErrors(true, false));
            return $this->redirectToRoute('fz_user_profile_edit');
        }