Php 如何使用serviceLocator将ZF2中的数据从控制器传递到窗体

Php 如何使用serviceLocator将ZF2中的数据从控制器传递到窗体,php,forms,controller,zend-framework2,Php,Forms,Controller,Zend Framework2,我需要将数据从控制器传递到字段集,如果我使用serviceLocator和FactoryInterface获取表单,我该如何做?有可能吗 当前我的文件如下所示: $options = array('eventid' => 1, 'matuserid' => 2); $form = new MultiMailFieldset($om, $options); // Set the hydrator $form->setHydrator(new Do

我需要将数据从控制器传递到字段集,如果我使用serviceLocator和FactoryInterface获取表单,我该如何做?有可能吗

当前我的文件如下所示:

    $options = array('eventid' => 1, 'matuserid' => 2);

    $form = new MultiMailFieldset($om, $options);
    // Set the hydrator
    $form->setHydrator(new DoctrineHydrator($om));
    $form->setObject(new Event());
控制器

$eventID    = $id;
$matuserID  = $this->zfcUserAuthentication()->getIdentity()->getId();

// DATA I would like to pass to the form
$dataDB     = array('eventid' => $eventID, 'matuserid' => $matuserID); 
// Get Form
$form     = $this->getServiceLocator()->get('mat_multimail_createform');
CreateFormFactory

public function createService(ServiceLocatorInterface $sm)
{ 
    $multimailFieldset = $sm->get('mat_multimail_fieldset');

    $form = new MultiMailCreateForm($multimailFieldset);
    // Set the hydrator
    $form->setHydrator(new DoctrineHydrator($sm->get('Doctrine\ORM\EntityManager')));

    return $form;
}
创造形式

public function __construct(FieldsetInterface $multimailFieldset)
{
    parent::__construct('create-multimail');

    //set the base fieldset
    $multimailFieldset->setUseAsBaseFieldset(true);
    $this->add($multimailFieldset);

    $this->add(array(
        'name'       => 'submit',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Speichern',
            'class' => 'btn btn-primary',
            'id'    => 'submultimail'
        )
    ));
}
CreateFieldsetFactory

public function createService(ServiceLocatorInterface $sm)
{
    $om = $sm->get('Doctrine\ORM\EntityManager');

    $form = new MultiMailFieldset($om, $options);
    // Set the hydrator
    $form->setHydrator(new DoctrineHydrator($om));
    $form->setObject(new Event());

    return $form;
}
public function createService(ServiceLocatorInterface $sm)
{
    $om = $sm->get('Doctrine\ORM\EntityManager');

    // add user id
    $authUser   = $sm->get('zfcuser_auth_service');
    $authUserId = $authUser->getIdentity()->getId();

    // add current eventID to fetch addresses
    $router      = $sm->get('router');
    $request     = $sm->get('request');
    $routerMatch = $router->match($request);

    $eventID = $routerMatch->getParam("id");

    $options = array('userid' => $authUserId, 'eventid' => $eventID);

    $form = new MultiMailFieldset($om, $options);
    // Set the hydrator
    $form->setHydrator(new DoctrineHydrator($om));
    $form->setObject(new Event());

    return $form;
}
字段集

public function __construct(ObjectManager $om, $options = array())
{
    parent::__construct('multimail');

    $this->add(array(
        'type' => 'Zend\Form\Element\Hidden',
        'name' => 'id'
    ));

    $this->add(array(
        'type' => 'Zend\Form\Element\Hidden',
        'name' => 'eventid'
    ));

    $this->add(array(
        'type'    => 'DoctrineModule\Form\Element\ObjectSelect',
        'name'    => 'extadressen',
        'attributes' => array(
            'class'     => 'form-control',
            'id'        => 'singleExtSel',
            'multiple'  => 'multiple',
        ),
        'options' => array(
            'object_manager' => $om,
            'target_class'   => 'Mat\Entity\AdressenExt',
            'property'       => 'email',
            'is_method'      => true,
            'find_method'    => array(
                'name'   => 'getExtAdressen',
                'params' => array(
                    'criteria' => array('userid' => $options['matuserid'], 'eventid' => $options['eventid']),
                ),
            ),
        ),
    ));
 public function __construct(ObjectManager $om, $options = array())
{
    parent::__construct('multimail');

    $this->add(array(
        'type' => 'Zend\Form\Element\Hidden',
        'name' => 'id'
    ));

    $this->add(array(
        'type' => 'Zend\Form\Element\Hidden',
        'name' => 'eventid'
    ));

    // add current eventID to fetch addresses
    $this->add(array(
        'type'    => 'DoctrineModule\Form\Element\ObjectSelect',
        'name'    => 'extadressen',
        'attributes' => array(
            'class'     => 'form-control',
            'id'        => 'singleExtSel',
            'multiple'  => 'multiple',
        ),
        'options' => array(
            'object_manager' => $om,
            'target_class'   => 'Mat\Entity\AdressenExt',
            'property'       => 'email',
            'is_method'      => true,
            'find_method'    => array(
                'name'   => 'getExtAdressen',
                'params' => array(
                    'criteria' => array('userid' => $options['userid'], 'eventid' => $options['eventid']),
                ),
            ),
        ),
    )); ...
或者我必须将控制器更改为类似以下内容:

    $options = array('eventid' => 1, 'matuserid' => 2);

    $form = new MultiMailFieldset($om, $options);
    // Set the hydrator
    $form->setHydrator(new DoctrineHydrator($om));
    $form->setObject(new Event());

谢谢

好吧,多亏了这篇文章,我才明白了! 我在工厂中获取参数并将其传递到字段集:

现场设置工厂

public function createService(ServiceLocatorInterface $sm)
{
    $om = $sm->get('Doctrine\ORM\EntityManager');

    $form = new MultiMailFieldset($om, $options);
    // Set the hydrator
    $form->setHydrator(new DoctrineHydrator($om));
    $form->setObject(new Event());

    return $form;
}
public function createService(ServiceLocatorInterface $sm)
{
    $om = $sm->get('Doctrine\ORM\EntityManager');

    // add user id
    $authUser   = $sm->get('zfcuser_auth_service');
    $authUserId = $authUser->getIdentity()->getId();

    // add current eventID to fetch addresses
    $router      = $sm->get('router');
    $request     = $sm->get('request');
    $routerMatch = $router->match($request);

    $eventID = $routerMatch->getParam("id");

    $options = array('userid' => $authUserId, 'eventid' => $eventID);

    $form = new MultiMailFieldset($om, $options);
    // Set the hydrator
    $form->setHydrator(new DoctrineHydrator($om));
    $form->setObject(new Event());

    return $form;
}
字段集

public function __construct(ObjectManager $om, $options = array())
{
    parent::__construct('multimail');

    $this->add(array(
        'type' => 'Zend\Form\Element\Hidden',
        'name' => 'id'
    ));

    $this->add(array(
        'type' => 'Zend\Form\Element\Hidden',
        'name' => 'eventid'
    ));

    $this->add(array(
        'type'    => 'DoctrineModule\Form\Element\ObjectSelect',
        'name'    => 'extadressen',
        'attributes' => array(
            'class'     => 'form-control',
            'id'        => 'singleExtSel',
            'multiple'  => 'multiple',
        ),
        'options' => array(
            'object_manager' => $om,
            'target_class'   => 'Mat\Entity\AdressenExt',
            'property'       => 'email',
            'is_method'      => true,
            'find_method'    => array(
                'name'   => 'getExtAdressen',
                'params' => array(
                    'criteria' => array('userid' => $options['matuserid'], 'eventid' => $options['eventid']),
                ),
            ),
        ),
    ));
 public function __construct(ObjectManager $om, $options = array())
{
    parent::__construct('multimail');

    $this->add(array(
        'type' => 'Zend\Form\Element\Hidden',
        'name' => 'id'
    ));

    $this->add(array(
        'type' => 'Zend\Form\Element\Hidden',
        'name' => 'eventid'
    ));

    // add current eventID to fetch addresses
    $this->add(array(
        'type'    => 'DoctrineModule\Form\Element\ObjectSelect',
        'name'    => 'extadressen',
        'attributes' => array(
            'class'     => 'form-control',
            'id'        => 'singleExtSel',
            'multiple'  => 'multiple',
        ),
        'options' => array(
            'object_manager' => $om,
            'target_class'   => 'Mat\Entity\AdressenExt',
            'property'       => 'email',
            'is_method'      => true,
            'find_method'    => array(
                'name'   => 'getExtAdressen',
                'params' => array(
                    'criteria' => array('userid' => $options['userid'], 'eventid' => $options['eventid']),
                ),
            ),
        ),
    )); ...