doutang7414 2016-06-30 07:19 采纳率: 0%
浏览 40
已采纳

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

I have a nested fieldset collection item inside another fieldset Ill that I am trying to set element variables for. I can successfully set elements for the Ill fieldset but am unable to generate or item fieldsets within the collection.

I have a very similar layout to this example: https://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html

The layout of nested forms is:
ILLForm(Form)->Ill(FieldSet)->Item(Collection/Fieldsets)
(the Ill can have many Item fieldsets)

Here is the Indexcontroller:

        $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 );

When I view the result in the view controller no items appear. Here is an example of the data I am trying to bind to the fieldsets:

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

It could be something simple that I have overlooked in the way I have structured the data but it eludes me.

  • 写回答

1条回答 默认 最新

  • dongqiaolong9034 2016-07-21 06:03
    关注

    OK so it seems the bind() function does not set the nested fieldset collections (but does everything else).

    There is another function that does not have this issue called setData(). I basically changed this part of the code:

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

    to this:

    //   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);
    }
    

    As per the comment in the code, if there is a session set then the data is pushed into the fields via the setData() function (and then validated). If its a fresh load then the model is bound to the fields via the bind() function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来