gpt4 book ai didi

doctrine-orm - Zend Framework 2 Form Collection Element 通过 Doctrine Entity 上的注解

转载 作者:行者123 更新时间:2023-12-04 04:47:09 25 4
gpt4 key购买 nike

我有一个安装/配置了 Doctrine 的 Zend Framework 2 项目。我直接在实体上使用 Zend Form 注释来生成表单来显示/编辑实体数据。我遇到了一个问题:

一个实体(供应商),具有以多对多关系配置的子实体(区域)集合。学说关系工作正常,但是我添加的 zend 形式注释

/*
* ...doctrine annotations...
* @Annotation\Type("Zend\Form\Element\Collection")
* @Annotation\Options({"label":"Territory", "target_element": {"type": "\DocurepVendor\Form\TerritoryFieldset"}})
*/

创建我创建的字段集对象,但不要将字段名称命名为数组。 (输入全部命名为 city 而不是 city[])所以当我提交更新表单时,映射器需要一个数组但只找到一个字符串并失败。

这是我的字段集代码。

TerritoryFieldset.php

namespace DocurepVendor\Form;

use \Zend\Form\Fieldset;
use \Zend\InputFilter\InputFilterProviderInterface;

class TerritoryFieldset extends Fieldset implements InputFilterProviderInterface {

public function __construct($name = null, $options = array()) {
parent::__construct('Locations', $options);
$this->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods())
->setObject(new \DocurepLocation\Entity\Territory());

$this->add(array(
'name' => 'city',
'options' => array(
'label' => 'City'
),
'attributes' => array()
))->add(array(
'name' => 'state',
'options' => array(
'label' => 'State'
),
'attributes' => array(
'required' => 'required'
)
));
}

public function getInputFilterSpecification() {
return array();
}

}

如果我将字段集中的名称设置为“city[]”而不是“city”,字段集中甚至不会填充学说数据。

我不确定这是否是我需要在子元素字段集中配置的东西,父元素的注释,完全在其他地方,或者如果这根本无法完成,我需要以另一种方式实现它。有人可以插入我朝着正确的方向前进吗?

最佳答案

我认为你应该使用 Doctrine Hydrator 而不是\Zend\Stdlib\Hydrator\ClassMethods()

use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;

..

$this->setHydrator(new DoctrineHydrator())

关于doctrine-orm - Zend Framework 2 Form Collection Element 通过 Doctrine Entity 上的注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17527415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com