在 zend 2 的嵌套字段集集合中设置数据 - Returns 空(父字段集有效)

Setting data in a nested fieldset collection in zend 2 - Returns empty (parent fieldset works)

我在另一个字段集 Ill 中有一个嵌套字段集集合 item,我正在尝试为其设置元素变量。我可以成功地为 Ill 字段集设置元素,但无法在集合中生成或 item 字段集。

我的布局与此示例非常相似:https://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html

嵌套表单的布局为:
ILLForm(表格)->Ill(FieldSet)->项目(Collection/Fieldsets)
(Ill 可以有很多 Item 字段集)

这是索引控制器:

        $ill = new Ill('ill', $this->ILLCategories, $this->campuses, $this->getFromOptions);

        $ill->setName($session->name);
        $ill->setEmail($session->email);

        $item_array = array();

        if ( isset($session->department))
        {
            $ill->setDepartment($session->department);
            $ill->setDivision($session->division);
           if ( isset($session->formData->items))
           {
               $item_iterator = $session->formData->items;
               $i = -1;
               foreach ( $item_iterator as $item)
               {
                   $i++;
                   $item_fieldset = new Item('Item '.($i+1), $this->ILLCategories, $this->getFromOptions);
                   $item_fieldset->setILLType($item->ILLType);
                   $item_fieldset->setGetFrom($item->getFrom);
                   $item_array[] = $item_fieldset;
               }
           }
           else
           {
               $item_fieldset = new Item('Item 1', $this->ILLCategories, $this->getFromOptions);
               $item_array[] = $item_fieldset;
           }

        }
        else
        {
            $item_fieldset = new Item('Item 1', $this->ILLCategories, $this->getFromOptions);
            $item_array[] = $item_fieldset;

        }

        $ill->items = $item_array;
        $this->ILLForm->bind( $ill );

当我在视图控制器中查看结果时,没有显示任何项目。这是我尝试绑定到字段集的数据示例:

object(ILL\Entity\Ill)[452]
  protected 'name' => string 'Test' (length=4)
  protected 'email' => string 'yay@yay.com' (length=11)
  protected 'division' => string 'Test' (length=4)
  protected 'department' => string 'Test' (length=4)
  protected 'contact' => null
  protected 'phone' => null
  protected 'idNumber' => null
  protected 'campus' => null
  public 'item' => 
    array (size=1)
      0 => 
        object(ILL\Entity\Item)[453]
          private 'ILLType' => null
          private 'requiredBy' => null
          private 'urgent' => null
          private 'citation' => null
          private 'copyright' => null
          private 'getFrom' => null

这可能是我在构建数据的方式中忽略的一些简单的事情,但它让我望而却步。

好的,所以 bind() 函数似乎没有设置嵌套的字段集集合(但会做其他所有事情)。

还有另一个函数 setData() 没有这个问题。我基本上更改了这部分代码:

$this->ILLForm->bind( $ill );

对此:

//   If previously submitted then run the validation to generate messages, otherwise bind autogenerated data to form (username etc...)
if ( isset($session->formData))
{
    $this->ILLForm->setData($session->formData);
    $this->ILLForm->isValid($ill);                           
}
else
{
    $this->ILLForm->bind($ill);
}

根据代码中的注释,如果存在会话集,则通过 setData() 函数将数据推送到字段中(然后进行验证)。 如果它是新负载,则模型通过 bind() 函数绑定到字段。