Php 如何在ZF2中的字段集工厂中使用集合

Php 如何在ZF2中的字段集工厂中使用集合,php,forms,doctrine-orm,zend-framework2,factory,Php,Forms,Doctrine Orm,Zend Framework2,Factory,我正在开发一个与ZF2和条令项目。我正试图在如中所示的形式创造中使用或。在该方法中,ObjectManager对象在控制器中创建,并在实例化时传递给新表单。当我想使用ZF2的FormElementManager时,将ObjectManager对象从控制器传递到表单会产生问题,因为ZF2要求我通过Zend\form\FormElementManager获取表单类的实例,而不是直接实例化它。为了解决这个问题,我根据问题的答案创建了表单和字段集工厂。问题答案中给出的方法适用于典型的字段集元素,但我需要

我正在开发一个与ZF2和条令项目。我正试图在如中所示的形式创造中使用或。在该方法中,ObjectManager对象在控制器中创建,并在实例化时传递给新表单。当我想使用ZF2的FormElementManager时,将ObjectManager对象从控制器传递到表单会产生问题,因为ZF2要求我通过Zend\form\FormElementManager获取表单类的实例,而不是直接实例化它。为了解决这个问题,我根据问题的答案创建了表单和字段集工厂。问题答案中给出的方法适用于典型的字段集元素,但我需要确定如何包含集合元素。本教程在父字段集中的collection元素中使用ObjectManager对象,我需要了解如何使用工厂添加集合

我试图模仿的教程中的TagFieldset: 新标记字段集工厂: 我试图模仿的教程中的BlogPostFieldset: 新BlogPostFieldsetFactory: 在module.config.php中: 当我在新的BlogPostFieldsetFactory中添加字段集元素时,我将替换原始字段集的以下代码:

$this->add(array(
        'type' => 'Zend\Form\Element\Text',
        'name' => 'title'
));
$tagFieldset = new TagFieldset($objectManager);
$this->add(array(
        'type'    => 'Zend\Form\Element\Collection',
        'name'    => 'tags',
        'options' => array(
                'count'           => 2,
                'target_element' => $tagFieldset
        )
));
为此:

$fieldset->add(array(
        'type' => 'Zend\Form\Element\Text',
        'name' => 'title'
));
$tagFieldset = new TagFieldset($objectManager);
$fieldset->add(array(
        'type'    => 'Zend\Form\Element\Collection',
        'name'    => 'tags',
        'options' => array(
                'count'           => 2,
                'target_element' => $tagFieldset
        )
));
如何从原始字段集中替换集合元素:

$this->add(array(
        'type' => 'Zend\Form\Element\Text',
        'name' => 'title'
));
$tagFieldset = new TagFieldset($objectManager);
$this->add(array(
        'type'    => 'Zend\Form\Element\Collection',
        'name'    => 'tags',
        'options' => array(
                'count'           => 2,
                'target_element' => $tagFieldset
        )
));

也许我误解了你的问题。。。。但是如果你换了这个

$this->add(array(
        'type' => 'Zend\Form\Element\Text',
        'name' => 'title'
));
其中:

$fieldset->add(array(
        'type' => 'Zend\Form\Element\Text',
        'name' => 'title'
));
那么您可能可以替换这个:

$tagFieldset = new TagFieldset($objectManager);
$this->add(array(
        'type'    => 'Zend\Form\Element\Collection',
        'name'    => 'tags',
        'options' => array(
                'count'           => 2,
                'target_element' => $tagFieldset
        )
));
为此:

$fieldset->add(array(
        'type' => 'Zend\Form\Element\Text',
        'name' => 'title'
));
$tagFieldset = new TagFieldset($objectManager);
$fieldset->add(array(
        'type'    => 'Zend\Form\Element\Collection',
        'name'    => 'tags',
        'options' => array(
                'count'           => 2,
                'target_element' => $tagFieldset
        )
));
现在,如果您无法将$objectManger传递给表单。。。好的,如果你看一下代码,你有这个可用的$serviceManager,这个东西看起来像一个DI容器,我相信你可以从那里得到$objectManager实例,如果不可用,你可以在里面放一个实例

最终代码的结尾可能如下所示:

$objectManager =  $serviceManager->get('DoctrineObjectManager') //or something like this
$tagFieldset = new TagFieldset($objectManager);
$fieldset->add(array(
        'type'    => 'Zend\Form\Element\Collection',
        'name'    => 'tags',
        'options' => array(
                'count'           => 2,
                'target_element' => $tagFieldset
        )
));

我很难理解你想做什么。。。您试图将集合元素添加到字段集吗?在我链接的教程中,控制器开发了一个
$objectManager
对象,该对象被传递到表单、
BlogPostFieldset
TagFieldset
。根据我链接到的问题,我无法将
$objectManager
传递给表单,因此我将表单和
BlogPostFieldset
替换为工厂。
BlogPostFieldset
中的代码使用
$objectManager
添加
$tagFieldset
集合,但我的替代品
BlogPostFieldsetFactory
没有
$objectManager
。如果我没有
$objectManager
,如何在
BlogPostFieldsetFactory
中开发
$tagFieldset
集合?
$objectManager =  $serviceManager->get('DoctrineObjectManager') //or something like this
$tagFieldset = new TagFieldset($objectManager);
$fieldset->add(array(
        'type'    => 'Zend\Form\Element\Collection',
        'name'    => 'tags',
        'options' => array(
                'count'           => 2,
                'target_element' => $tagFieldset
        )
));