从eventSubscriber获取实体

时间:2017-05-24 08:47:53

标签: php symfony events doctrine

我正在使用eventSubscriber动态地将一个字段(Paciente)加载到一个表单中,在我需要获取的preSubmit函数中除了paciente的ID,即paciente的dni之外。我可以直接获取id,但是dni需要带实体,我不知道如何从这里开始。

我的相关事件如下:

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

编辑方法1使用LifecycleEventArgs预先提交:

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

方法的行声明中的错误:

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

1 个答案:

答案 0 :(得分:0)

创建一个工厂,将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;
    }
}

注册

# 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 }

并向您的订阅者添加构造函数

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

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

    // ...
}

让我知道这是否足够清楚

有关详情,请查看:http://symfony2-document.readthedocs.io/en/latest/cookbook/service_container/factories.html