基于 Zend_Form 数组的元素设置和检索
我有两个实体参与这个问题。用户可以举办一个与多个设备相关的活动。我需要一个表格,用户可以在该特定事件中输入该设备的工作时间和投资回报率。在这种情况下,设备实际上是其他两个实体(设备和事件)中间的一个实体,用于创建具有额外参数的多对多。因此设备具有“小时”和“投资回报率”字段。我想让我的表单动态添加一个字段,用于表示活动中每件设备的小时数和投资回报率。我可以完成这部分。我遇到问题的部分是将元素添加到表单中。我一直在查看试图帮助我的页面: Zend_Form - 基于数组的元素?< /a>.
然而,在这个问题上,他们似乎并没有做我想做的事情。
这就是我现在所拥有的:
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'])
));
}
但是,使用这种方法,只显示最后一件设备的信息。如果有我没有想到的设置方法,请告诉我。我只需要能够在最后解析一组数据,然后就可以从那里获取它。
提前感谢您的帮助。
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.
Here's what I have right now:
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.
Thanks for your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在每个循环过程中添加相同的元素。
addElement
的第二个参数是元素标识符(在您的情况下为roi
和hours
)。可能的替代方案如下:(
通过将 ID 附加到每个元素名称/标识符)。
可能还有其他解决方案,但您始终需要为添加到表单的每个元素拥有唯一的标识符。
希望有帮助,
You are adding the same element every loop pass. The second parameter to
addElement
is the element identifier (roi
andhours
in your case).A possible alternative could be the following:
(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.
Hope that helps,