Php 如何清除ZF2表单集合中的数据?
我有一个带有表单集合元素的ZF2表单。集合元素的名称为“角色”。如果我这样做:Php 如何清除ZF2表单集合中的数据?,php,zend-framework,zend-framework2,zend-form,zend-form-element,Php,Zend Framework,Zend Framework2,Zend Form,Zend Form Element,我有一个带有表单集合元素的ZF2表单。集合元素的名称为“角色”。如果我这样做: $form->setData(array('roles' => array('admin', 'moderator'))); 然后我会: $form->setData(array('roles' => array())); // it does not work 如果我们这样做: $form->get('roles')->getCount(); // 2 还有两个元素。我需要
$form->setData(array('roles' => array('admin', 'moderator')));
然后我会:
$form->setData(array('roles' => array())); // it does not work
如果我们这样做:
$form->get('roles')->getCount(); // 2
还有两个元素。我需要清除此集合中的元素
这对我来说非常重要,因为我使用的是教义2。所以我应该在将数据设置到表单之前绑定非空实体。例如:
$form->bind($entity); // $entity->roles is not empty
$form->setData($someData); // $someData['roles'] is empty array
if ($form->isValid()) {
$form->get('roles')->getCount(); // 2(!) it is not empty!
saveToDb($entity);
}
return $form;
请显示将“角色”集合元素附加到表单的位置;应该有一个选项“count”,它是初始集合计数。当我创建集合时,我不设置计数。默认情况下,计数==1。当我执行#form->bind($entity)$entity有两个角色时,这就是为什么count==2。当我执行$form->setData(['roles'=>[]])时,角色集合计数仍然等于2。这是一个问题。如果您查看Collection::populateValues()方法,您将看到,如果数据为空,那么它不会执行任何操作。为什么不取消设置实体上的roles属性?