Doctrine orm ZF2注释原则:如何清除多选字段?

Doctrine orm ZF2注释原则:如何清除多选字段?,doctrine-orm,zend-framework2,Doctrine Orm,Zend Framework2,我的多选字段有问题。代码如下: 在实体类中 /** * @Annotation\Options({ "disable_inarray_validator":"true", "label":"Bound Checkpoints", "target_class":"Checkpoint","property":"name"}) * @Annotation\Type("DoctrineORMModule\Form\Element\EntitySelect") * @Annotation\Attr

我的多选字段有问题。代码如下:

在实体类中

/**
 * @Annotation\Options({ "disable_inarray_validator":"true", "label":"Bound Checkpoints", "target_class":"Checkpoint","property":"name"})
 * @Annotation\Type("DoctrineORMModule\Form\Element\EntitySelect")
 * @Annotation\Attributes({ "multiple":"true", "class":"form-control"})
 * @Annotation\Required(false)                     
 * @ORM\ManyToMany(targetEntity="Checkpoint", inversedBy="affectedByCheckpoints")
 */
private $boundCheckpoints;


public function __construct() {
        $this->boundCheckpoints= new \Doctrine\Common\Collections\ArrayCollection();

    }


   public function addBoundCheckpoints( $items)
    {
        foreach($items as $item)
        {
            $this->boundCheckpoints->add($item);
        }
    }

    public function removeBoundCheckpoints($items)
    {

        foreach($items as $item)
        {
            $this->boundCheckpoints->removeElement($item);
        }
    }
我的问题是:如果我在字段中设置一些内容,它会很好地保存它。如果我移除了一个,仍然可以工作。但是,如果我删除我设置的所有项并启动表单,则永远不会调用removeBoundCheckpoints方法

我试图为表单设置一个必需的验证器,但如果我这样做,我就会遇到一个验证问题


有什么想法吗

通过将此变通方法添加到模板中解决

{% if field.getAttribute('multiple') == 'multiple' or field.getAttribute('multiple') == 'true' %} 
      <input type="hidden" name="{{field.getName()}}" value="" /> 
{% endif %} 
{{formElement(field) }}

当提交为空时,您可能会发现post中缺少表单元素。在本例中,您需要使用空数组值将其添加回。您是对的,我没有检查post数组。。正如你所说,钥匙不见了。。我在模板中添加了一些变通方法。看起来很有效。感谢您的帮助: