I am using an eventSubscriber to dynamically load a field (Patient) in a form, of which in its preSubmit
function I need to obtain in addition to the id the patient's ID. The id can be obtained directly, but the ID I need to bring the entity, and I do not know how I can do it from here.
My event in question is the following:
class AddHistoriaClinicaFieldSubscriber implements EventSubscriberInterface
{
private $propertyPathToHistoriaClinica;
public function __construct($propertyPathToHistoriaClinica)
{
$this->propertyPathToHistoriaClinica = $propertyPathToHistoriaClinica;
}
public static function getSubscribedEvents()
{
return array(
FormEvents::POST_SET_DATA => 'preSetData',
FormEvents::PRE_SUBMIT => 'preSubmit'
);
}
private function addHistoriaClinicaForm($form, $paciente_id)
{
$formOptions = array(
'class' => 'BiobancoBundle:HistoriaClinica',
'empty_value' => '-- SELECCIONAR HISTORIA CLINICA --',
'label' => 'Historia Clínica',
'attr' => array(
'class' => 'historia_clinica_selector',
),
'query_builder' => function (EntityRepository $repository) use ($paciente_id) {
$qb = $repository->createQueryBuilder('h')
->innerJoin('h.paciente', 'p')
->where('p.id = :p')
->setParameter('p', $paciente_id)
;
return $qb;
},
);
$form->add($this->propertyPathToHistoriaClinica, 'entity', $formOptions);
}
public function preSetData(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
$accessor = PropertyAccess::createPropertyAccessor();
$h = $accessor->getValue($data, $this->propertyPathToHistoriaClinica);
$paciente_id = ($h) ? $h->getPaciente()->getNumeroIdentificacion() : null;
$this->addHistoriaClinicaForm($form, $paciente_id);
}
public function preSubmit(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
$paciente_id = array_key_exists('paciente', $data) ? $data['paciente'] : null;
//AQUÍ ES DONDE NECESITO OBTENER EL DNI, PARA PASARLO AL FORMULARIO
//dump($data);die();
$this->addHistoriaClinicaForm($form, $paciente_id);
}
}
EDIT 1 OF preSubmit:
use Doctrine\ORM\Event\LifecycleEventArgs;
...
public function preSubmit(FormEvent $event, LifecycleEventArgs $args)
{
$data = $event->getData();
$form = $event->getForm();
$paciente_id = array_key_exists('paciente', $data) ? $data['paciente'] : null;
dump($args->getEntityManager()->getRepository("BiobancoBundle:Paciente")->find($paciente_id));die();
$this->addHistoriaClinicaForm($form, $paciente_id);
}
The error, jumps in the line of the declaration of the function, when injecting LifecycleEventArgs