简体   繁体   English

从eventSubscriber获取实体

[英]Get an entity from eventSubscriber

I'm using an eventSubscriber to dynamically load a field (Paciente) into a form, in its preSubmit function I need to get in addition to the ID of the paciente, the dni of paciente. 我正在使用eventSubscriber将字段(Paciente)动态地加载到表单中,在其preSubmit函数中,除了paciente的ID(paciente的dni)之外,我还需要获取它。 The id I can get directly, but the dni need to bring the entity, and I do not know how I can do it from here. 我可以直接获取id,但是dni需要携带实体,我不知道如何从这里开始。

My event in question is as follows: 我所讨论的事件如下:

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;
        //HERE IS WHERE I NEED TO OBTAIN THE DNI, TO PASS IT TO THE FORM
        //dump($data);die();
        $this->addHistoriaClinicaForm($form, $paciente_id);
    }
}

EDIT 1 OF METHOD preSubmit with LifecycleEventArgs : 使用LifecycleEventArgs编辑方法preSubmit的1:

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);
    }

ERROR in line declaration of method: 方法的行声明中存在错误:

Catchable Fatal Error: Argument 2 passed to BiobancoBundle\Form\EventListener\AddHistoriaClinicaFieldSub‌​scriber::preSubmit() must be an instance of Doctrine\ORM\Event\LifecycleEventArgs, string given.

create a factory to inject the EntityManager or Repository into your subscriber. 创建一个工厂,将EntityManagerRepository注入您的订户。

class AddHistoriaClinicaFieldSubscriberFactory
{
    public static function create($entityManager)// typehint this
    {
        // You could retrieve the repo here, so you don't pass the whole em to the instance
        $instance = new AddHistoriaClinicaFieldSubscriber($entityManager);

        // ...

        return $instance;
    }
}

Register it 注册

# app/config/services.yml

services:
# ...

    app.add_historia_clinica_field_subscriber_factory:
    class: YOURNAMESPACE\AddHistoriaClinicaFieldSubscriberFactory

    app.add_historia_clinica_field_subscriber:
        class:     YOURNAMESPACE\AddHistoriaClinicaFieldSubscriber
        factory:   'add_historia_clinica_field_subscriber_factory:create'
        arguments: ['@doctrine.orm.default_entity_manager']
        tags:
        - { name: WHATEVERYOUHAVEHERE }

And add a constructor to you Subscriber 并向您添加一个构造函数

class AddHistoriaClinicaFieldSubscriber implements EventSubscriberInterface
{
    // ...
    protected $entityManager;

    public function __construct($entityManager) {// typehint this
        $this->entityManager = $entityManager;
    }

    // ...
}

Let me know if this is clear enough 让我知道这是否足够清楚

For more info check: http://symfony2-document.readthedocs.io/en/latest/cookbook/service_container/factories.html 有关更多信息,请访问: http : //symfony2-document.readthedocs.io/en/latest/cookbook/service_container/factories.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM