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 :
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,
]);
}
}