Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Symfony 5.4 error: Object of class App\Entity\Routes could not be converted to string

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

    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. Can anyone spot the error and help me fix it?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Hi NoobDeveloper69, you can try the following.

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,
        ]);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading