KevSIO
KevSIO

Reputation: 21

Symfony 5 - Object of class App\Entity\Vegetal could not be converted to string

I have a problem when trying to add an image to a folder on a website that I'm developping currently. When I try to rename the file, I would like it to be : idOfMyObject.jpg

But when I want to add it on my form, this error code pops up : "Object of class App\Entity\Vegetal could not be converted to string".

The problem's apparently inside the isset, but I can't find a solution to convert my id to string.

Here is the Controller :

public function ajouterVegetal(Request $request): Response
    {   
        $fFeuille = new FormeFeuille();
    
        $vegetal = new Vegetal();
        $formVegetal = $this->createForm(VegetalType::class, $vegetal);
        $formVegetal->handleRequest($request);

        //Si le formulaire est bien valide (tous les champs remplis) et que le bouton submit est appuyé
        if ($formVegetal->isSubmitted() && $formVegetal->isValid()) { 
            //on récupère les données du formaulaire
            $vegetal = $formVegetal->getData();
            $entityManager = $this->getDoctrine()->getManager();
            
            //on récupère les données de la variable image dans le formulaire d'ajout     
            $fileDownload = $formVegetal['images']->getData();
            //si le fichier existe, on le renomme avec l'id du végétal, pour le sauvegarder dans le dossier configuré dans service.yaml
            if (isset($fileDownload))
            {
                $nomImage = $fFeuille->getVegetals()->{$this->$vegetal->getId()}.'.'.$fileDownload->guessExtension();         
                $fileDownload->move(
                     $this->getParameter('upload_directory'),
                     $nomImage
                );
            }
            //on ajoute les données dans la base
            $entityManager->persist($vegetal);
            $entityManager->flush();

            $this->getDoctrine()->getManager()->flush();      

            return $this->redirectToRoute('listerVegetal');
        } else {
            return $this->render('Vegetal/ajouterVegetal.html.twig', [
                'vegetal' => $vegetal,
                'form' => $formVegetal->createView(),
            ]);
        }
    }

Upvotes: -1

Views: 2704

Answers (1)

emomaliev
emomaliev

Reputation: 2383

I will allow myself to assume that this error is caused when rendering the form. And most likely you are using EntityType for Vegetal in formBilder. I'm guessing that when you render the form, you get this error because formBilder tries to convert Vegetal to render it in "select", but it fails because it doesn't know what value to take.


The code below will just help php decide what value to return when converting this object to a string.

Add in Entity Vegetal

public function __toString(): string
{
    return $this->getName();  // or some string field in your Vegetal Entity 
}

It also might not work for you, but usually my similar errors were solved by this.

Upvotes: 2

Related Questions