Zend_Form 基于数组的元素设置和检索 [英] Zend_Form Array Based Element Setup and Retreival

查看:24
本文介绍了Zend_Form 基于数组的元素设置和检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体参与了这个问题.用户可以拥有一个与多个设备相关联的事件.我需要一个表单,用户可以在该特定事件中输入该设备的小时数和投资回报率.在这种情况下,设备实际上是位于其他两个实体(设备和事件)中间的一个实体,用于创建具有额外参数的多对多.所以设备有字段小时"和投资回报率".我想让我的表单为活动中的每件设备动态添加一个小时和 roi 字段.我可以到这部分.我有问题的部分是将元素添加到表单中.我一直在寻找帮助我的页面:Zend_Form - 基于数组的元素?.

I have two entities involved in this issue. A user can have an event that has multiple pieces of equipment tied to it. I need a form that the user can enter hours and roi for that piece of equipment at that particular event. Equipment in this case is actually an entity in the middle of two other entities (equipment and event) to create a many to many with extra parameters. So equipment has the fields 'hours' and 'roi'. I would like to have my form dynamically added a field for hours and roi for each piece of equipment on the event. I can get up to this part. The part I have issues, is adding the elements to the form. A page that I've been looking at to try and help me: Zend_Form - Array based elements?.

然而,在那个问题上,他们似乎并没有做我想做的事情.

However, in that question, they don't seem to be doing the same thing I wish to do.

这是我现在所拥有的:

foreach ($event['equipment'] as $equipment)
{
  $form->addElement('text', 'roi', array(
    'label' => $equipment['equipment']['model'] . ' ROI', 
    'required' => true,
    'belongsTo' => strval($equipment['id'])
  ));
  $form->addElement('text', 'hours', array(
    'label' => $equipment['equipment']['model'] . ' Hours', 
    'required' => true,
    'belongsTo' => strval($equipment['id'])
  ));
}

但是,使用这种方法,只显示最后一条设备的信息.如果有我没有想到的设置方法,请告诉我.我只需要能够在最后解析一组数据,然后就可以从那里获取它.

However, with this method, only the last piece of equipment's information is shown. If there's a way to set this up that I'm not thinking of, please let me know. I just need to be able to parse through an array of data at the end and I'll be able to take it from there.

提前感谢您的帮助.

推荐答案

您在每次循环中添加相同的元素.addElement 的第二个参数是元素标识符(在您的情况下为 roihours).

You are adding the same element every loop pass. The second parameter to addElement is the element identifier (roi and hours in your case).

可能的替代方案如下:

foreach ($event['equipment'] as $equipment)
{
  $form->addElement('text', 'roi' . $equipment['id'] , array(
    'label' => $equipment['equipment']['model'] . ' ROI', 
    'required' => true,
    'belongsTo' => strval($equipment['id'])
  ));
  $form->addElement('text', 'hours' . $equipment['id'], array(
    'label' => $equipment['equipment']['model'] . ' Hours', 
    'required' => true,
    'belongsTo' => strval($equipment['id'])
  ));
}

(通过将 ID 附加到每个元素名称/标识符).

(by appending the ID to each element name/identifier).

可能还有其他解决方案,但您始终需要为添加到表单的每个元素拥有唯一标识符.

There could be other solutions, but you always need to have unique identifiers for each element you add to the form.

希望有所帮助,

这篇关于Zend_Form 基于数组的元素设置和检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆