I'm new in symfony2 ver 2.7: I would like to create registration number in form with data from count rows in existing table.
It's OK when i create in PatientController and show the result in twig format. But i need show the result in text type form. I write in PatientRepository a function:
public function getNumberPatient()
{
$qb = $this->createQueryBuilder('name')
->select('COUNT(name)');
return (int)$qb->getQuery()
->getSingleScalarResult();
}
And in my Entity file to generate like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('dateBirth', 'date', array(
'years' => range(date('Y'), date('Y', strtotime('-50 years'))),
'required' => TRUE,
))
->add('regCode', 'text', array(
'required' => TRUE,
'data' => function(PatientRepository $r){
return $r->getNumberPatient();
},
I have a trouble when i call this function from PatientEntity. Is it Possible?
I did create function with:
'query_builder' => function(PatientRepository $r){
return $r->getNumberPatient();
but it give an error
The option "query_builder" does not exist. Defined options are: "action",
Please help me..
There are multiple ways to do it:
example in controller:
MyFormType:
example:
$options
array, like this$entityManager = $options['entityManager']
, or you can even pass the entityManager to constructor.. Here you can see it..