基于 Zend_Form 数组的元素设置和检索

发布于 2024-12-05 01:56:38 字数 950 浏览 0 评论 0原文

我有两个实体参与这个问题。用户可以举办一个与多个设备相关的活动。我需要一个表格,用户可以在该特定事件中输入该设备的工作时间和投资回报率。在这种情况下,设备实际上是其他两个实体(设备和事件)中间的一个实体,用于创建具有额外参数的多对多。因此设备具有“小时”和“投资回报率”字段。我想让我的表单动态添加一个字段,用于表示活动中每件设备的小时数和投资回报率。我可以完成这部分。我遇到问题的部分是将元素添加到表单中。我一直在查看试图帮助我的页面: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2024-12-12 01:56:38

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

可能的替代方案如下:(

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 附加到每个元素名称/标识符)。

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

希望有帮助,

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

A possible alternative could be the following:

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'])
  ));
}

(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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文