1

I am beginning to learn symfony4. I am stuck with the problem that is to update the existing data in the database by using Symfony form. The problem occurs with the file to set by the member function which is shown in the code. Have any solution? please resolve

The file move successfully but no set with the member function Here is the code

enter image description here

/**
 * @Route("/new", name="new")
 */
public function new()
{
    return $this->render('dashboard/new.html.twig', [
        'controller_name' => 'DashboardController',
    ]);
}[enter image description here][1]

/**
 * @Route("/edit/{username}", name="edit")
 */
public function edit(Request $request, $username)
{   
    $user = new User();
    $user = $this->getDoctrine()->getRepository(User::class)->find($username);
    $imageConstraints = [
        new Image([
            'maxSize' => '5M'
        ])
    ];
    $form = $this->createFormBuilder($user)
        ->add('name', TextType::class)
        ->add('username', TextType::class)
        ->add('bio', TextType::class)
        ->add('location', TextType::class)
        ->add('website', UrlType::class)
        ->add('dob', BirthdayType::class)
        ->add('dp', FileType::class, [
            'multiple' => false,
            'mapped' => false,
            'required' => false,
            'constraints' => $imageConstraints
        ])
        ->add('header_pic', FileType::class, [
            'required' => false,
            'multiple' => false,
            'mapped' => false,
            'constraints' => $imageConstraints
        ])
        ->add('save', SubmitType::class)
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $entityManager = $this->getDoctrine()->getManager();
        /**@var UploadedFile $uploadedFile */
        //dd($form['dp']->getData());

        $destination = $this->getParameter('kernel.project_dir').'/public/uploads';

        //for Dp means Profile pic
        $uploadedFileDp = $form['dp']->getData();
        $originalFilenameDp = pathinfo($uploadedFileDp->getClientOriginalName(), PATHINFO_FILENAME);
        $newFilenameDp = $originalFilenameDp.'-'.uniqid().'.'.$uploadedFileDp->guessExtension();
        $uploadedFileDp->move(
            $destination,
            $newFilenameDp
        );
        $user->setDp($newFilenameDp);

        //Header pic
        $uploadedFileHeaderPic = $form['header_pic']->getData();
        $originalFilenameHeaderPic = pathinfo($uploadedFileHeaderPic->getClientOriginalName(), PATHINFO_FILENAME); 
        $newFilenameHeaderPic = $originalFilenameHeaderPic.'-'.uniqid().'.'.$uploadedFileHeaderPic->guessExtension();

        $uploadedFileHeaderPic->move(
            $destination,
            $newFilenameHeaderPic
        );
        $user->setHeaderPic($newFilenameHeaderPic);
        $entityManager->flush();

        // do anything else you need here, like send an email

        return $this->redirectToRoute('new');
    }
    return $this->render('dashboard/edit.html.twig', array
        ('form' => $form->createView())
    );
  }
}

Call to a member function setDp() on null

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
babloo
  • 55
  • 2
  • 7
  • your `$user = $this->getDoctrine()->getRepository(User::class)->find($username);` must be failing and returning null. See https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12769983#12769983 – Scuzzy Aug 02 '19 at 04:55
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Scuzzy Aug 02 '19 at 04:55
  • Unless you have overridden the repository `find()` method it expects id as parameter not username – Arleigh Hix Aug 02 '19 at 07:12

2 Answers2

0

Your problem is here:

$user->setDp($newFilenameDp);

The error is telling you that the setDp can't be called against null, therefore what that then means is $user must be null and not an instance of the user object you are expecting.

You have this line of code:

$user = $this->getDoctrine()->getRepository(User::class)->find($username);

It is possible that your find is failing and returning you the null, ie you're failing to actually locate the user and populate $user with your object. You should be throwing an exception in your edit method or aborting at this point.


$user = $this->getDoctrine()->getRepository(User::class)->find($username);
if( $user === null or $user instanceof WhatObjectYouExpect === false )
{
   throw new Exception('Failed to load user data');
}
Scuzzy
  • 12,186
  • 1
  • 46
  • 46
  • please explore more how to throw an exception? – babloo Aug 02 '19 at 06:03
  • Thanks for giving the answer... but it shows me the exception message. – babloo Aug 02 '19 at 14:59
  • I don't know how to explain it any differently, `find()` is failing with the `$username` value you are providing it. You're performing a search with some information that isn't returning what you expect, so figure out what is wrong with the input. – Scuzzy Aug 02 '19 at 22:05
0

I found the solution, which is to pass the primary key $id instead of $username in the route which will not give the null value. Here is the twig hyperlink to pass the user id to update the post.

<a href="edit/{{app.user.id}}">Edit</a>
babloo
  • 55
  • 2
  • 7