I am trying to add Select form field by using values in DB table. In the official documentation it is described as in the picture below:
I did implemented in the same way but I am getting Expection:
The "App\Entity\Definition" entity has a repositoryClass set to "App\Entity\DefinitionRepository", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "doctrine.repository_service".
My FormType code:
namespace App\Form;
use App\Entity\Company;
use App\Entity\Definition;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CompanyDetailType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null, ['label' => 'Company Name'])
->add('type', EntityType::class, [
'class' => Definition::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('d');
},
'choice_label' => 'name',
'label' => 'Company Type'
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Company::class,
]);
}
}
My DefinitionRepository:
namespace App\Repository;
use App\Entity\Definition;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class DefinitionRepository extends ServiceEntityRepository
{
}
the $type field of Company Entity:
/**
* @var Definition
*
* @ORM\ManyToOne(targetEntity="App\Entity\Definition")
*/
private $type;