Get an entity from in eventSubscriber

0

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

    
asked by Ivan Javier Barranco Gavilan 24.05.2017 в 09:56
source

1 answer

1

In the official symfony documentation you will find this to call the entity from eventSubscriber

public function getSubscribedEvents()
{
    return array(
        'postPersist',
        'postUpdate',
    );
}

public function postUpdate(LifecycleEventArgs $args)
{
    $this->index($args);
}

public function postPersist(LifecycleEventArgs $args)
{
    $this->index($args);
}

public function index(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    // perhaps you only want to act on some "Product" entity
    if ($entity instanceof Product) {
        $entityManager = $args->getEntityManager();
        // ... do something with the Product
    }
}

Simply calling the entity you could invoke it and then changing the parameters indicated by the documentation should work

I have found more explicit documentation, maybe adding all the methods stop jumping the error

    
answered by 24.05.2017 в 10:46