Doctrine orm Zf2表单字段集不返回任何字段

Doctrine orm Zf2表单字段集不返回任何字段,doctrine-orm,zend-framework2,Doctrine Orm,Zend Framework2,我已经使用ZF2和Doctrine2完成了很多项目。我用如下方式构建表单:创建表单类扩展表单,然后创建字段集并将其设置为基本字段集,然后在字段集中添加字段。在module.php中,我在formElementConfig中为表单创建工厂。直到现在,它一直是这样工作的。我创建了一个新项目,突然我遇到了一个问题,我无法找到正在发生的事情。这是我的密码 //module.php public function getFormElementConfig() { return

我已经使用ZF2和Doctrine2完成了很多项目。我用如下方式构建表单:创建表单类扩展表单,然后创建字段集并将其设置为基本字段集,然后在字段集中添加字段。在module.php中,我在formElementConfig中为表单创建工厂。直到现在,它一直是这样工作的。我创建了一个新项目,突然我遇到了一个问题,我无法找到正在发生的事情。这是我的密码

 //module.php
 public function getFormElementConfig()
    {
        return array(
            'factories' => array(
                'OfferForm' => function($sm) {
                    $locator = $sm->getServiceLocator();
                    $form = new \Application\Form\OfferForm();
                    $form->setServiceLocator($locator);
                    return $form;
                },
            )
        );
    }

//Form
class OfferForm extends Form implements ServiceLocatorAwareInterface
{
    protected $serviceLocator;

    public function init()
    {
        $this->setAttributes(array(
            'id' => 'offer',
            'method' => 'post',
            'class'  => 'custom',
            'enctype' => 'multipart/form-data'
        ));

        $this->setAttribute('method', 'post')
            ->setHydrator(new ClassMethodsHydrator(false))
            ->setInputFilter(new InputFilter());

        $this->add(array(
            'name' => 'offer',
            'type' => 'Application\Form\Fieldset\OfferFieldset',
            'options' => array(
                'use_as_base_fieldset' => true
            )
        ));

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

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'id' => 'submit',
                'type' => 'submit',
                'value' => $this->getServiceLocator()->getServiceLocator()->get('translator')->translate('Submit offer'),
                'class' => 'btn btn-info'
            )
        ));
    }
....

//Fieldset

class OfferFieldset extends Fieldset implements InputFilterProviderInterface, ServiceLocatorAwareInterface
{
    public function init()
    {
        $this->setHydrator(new ClassMethodsHydrator(false))
            ->setObject(new Offer());

        $this->add(array(
            'name' => 'title',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'required' => 'required',
                'class' => 'form-control',
            )
        ));
        ....other fileds
    }
    /**
     * @return array
     */
    public function getInputFilterSpecification()
    {
      ....
    }
}


//Controller

$em = $this->getObjectManager();
        $offer = new Offer();
        $form = $this->getServiceLocator()->get('FormElementManager')->get('OfferForm');
        $form->setHydrator(new DoctrineHydrator($em, 'Application\Entity\Offer'))->bind($offer);
        if ($this->request->isPost()) {
            $form->setData($this->request->getPost());
            if ($form->isValid()) {
                var_dump('ok');
            }
        }
        $form->prepare();
        return new ViewModel(array(
            'form' => $form,
        ));
到目前为止,这种做事方式一直对我有效。如果我试图用$this->form->get('offer')->get('title')在Veiw中获取表单元素,它会说没有名为'title'的字段

我注意到的一件事是,当在控制器中调用form($form=$this->getServiceLocator()->get('FormElementManager')->get('OfferForm');)时,不会调用设置所有字段的字段集方法init()。 我试图将数据转储到那里并终止应用程序,但它根本没有进入方法


我可以提供更多的代码,但我认为这都是关于构建表单的

您还需要将字段集添加到formelementmanager配置中。经理的初始值设定项将调用fieldset init()方法。

是的,可以。但我现在真的很困惑,因为在我过去的项目中,我从来没有定义过字段集和每次工作的字段集。甚至嵌套的字段集。很奇怪嘿,伙计,看看现在发生了什么。。。。当我调用$this->formRow($form->get(..)->get(element))时,输出非常奇怪。而且它没有得到正确的元素。我把ffor examle元素称为“name”,它输出元素“price”。。。。它实际上不起作用。。。。有些事情真的很疯狂。。。如果我有2个元素,它只得到最后一个元素,显示两次,在元素名称中它使它提供[offer[element]]。如果元素为3,则输出3个相等的元素offer[offer[lastelement]]。但这还不是全部。如果我有3个文本元素和2个选择元素,它将生成3个相等的文本字段和2个相等的选择。使用最后一种uniqe类型。这是一件非常疯狂和奇怪的事情。您过去是如何将元素添加到字段集的?使用构造函数还是init()方法?关于这个奇怪的命名,看起来像是在进行某种递归。我还没发现你的代码有什么奇怪的地方。最好的办法是简化代码,直到它工作为止,使用xdebug逐步完成代码,或者将此代码与其他工作表单进行比较。我给你的这个例子是一个正确的例子,适用于所有其他项目,直到现在。我从来没有遇到过这样的问题。