0
namespace App\Form;

use App\Entity\Booking;
use App\Entity\User;
use App\Entity\Routes;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;


class BookingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('date')
            ->add('time')
            ->add('user', EntityType::class, [
                'class' => User::class,
                'choice_label' => 'username', // Replace 'username' with the actual property of User to be displayed
            ])
            ->add('routes', EntityType::class, [
                'class' => Routes::class,
                'choice_label' => 'name', // Replace 'name' with the actual property of Routes to be displayed
            ])
        ;

        // Deliberate error: Typo in method name
        $builder
            ->addr('missingMethod'); // This line has a typo in the method name 'addr' instead of 'add'
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Booking::class,
        ]);
    }
}

I made a CRUD controller using make:crud but when I try to add a new row in my SQL database I get this error: Object of class App\Entity\Routes could not be converted to string

Where is the error and how do I fix it?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • A [mre] includes: cut & paste & runnable code including initialization; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. For SQL include DDL & tabular initialization code. For debug that includes the least code you can give that is code that you show is OK extended by code that you show is not OK. [ask] [Help] – philipxy May 09 '23 at 03:29

2 Answers2

1

Symfony is trying to convert your entity into a string that "summarizes" it for display in forms.
You should be adding a __toString() method that returns a string in the class of the entity the error appears on. It could look like that (adapt code to your properties and needs) :

public function __toString()
{
    $id = $this->getId();
    $name = $this->getName();
    return "$id | $name";
}

It will allow Symfony to display the "summary" of your entity the types of fields that require that data, for example in dropdowns. Hope this helps.

0

$builder->addr(), which is an invalid method name. Will cause a fatal error when executing the code. This is the right code:

<?php

namespace App\Form;

use App\Entity\Booking;
use App\Entity\User;
use App\Entity\Routes;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;


class BookingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('date')
            ->add('time')
            ->add('user', EntityType::class, [
                'class' => User::class,
                'choice_label' => 'username', // Replace 'username' with the actual property of User to be displayed
            ])
            ->add('routes', EntityType::class, [
                'class' => Routes::class,
                'choice_label' => 'name', // Replace 'name' with the actual property of Routes to be displayed
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Booking::class,
        ]);
    }
}