Zend Framework:以一种形式将多个数据库记录处理为复选框

发布于 2024-08-22 01:48:00 字数 644 浏览 4 评论 0原文

我是 zend 框架的新手,但已经成功地迈出了我的第一步。 到目前为止,我已经创建了一些 Zend_Forms,它们映射我的模型的单个记录 到表单字段。我已经为每种情况处理了带有表单类的表单。 到目前为止,这一切都运作良好。

现在我遇到的情况是我必须为产品分配功能。功能和产品是我的应用程序的一部分。特征存储在我的数据库中的三个表中。 对于每个功能,第三个表中都有一条记录

  • 第一个是功能组,其中保存功能组的名称。每个功能都应该分配到一个功能组。

  • 第二个表是功能表。该表有一个指向功能组和功能名称的外键。

  • 第三个表是某种将功能与产品连接起来的多对多关系。该表有一个附加字段,其中包含产品的这一独特功能的可选值(除了两个外键之外)。

例如:如果产品重量为 4,78 kg,则值“4,78”存储在第三个表中,标签“重量 %s kg”存储在第二个表中。特征组可能类似于“物理属性”,保存在第一个表中。

长话短说: 我的问题是如何处理必须以一种形式创建和编辑多个数据库记录的情况。 计划是有一个表单,其中每个功能都有许多复选框,从而按主题对功能进行分组。每个复选框都应该有一个附加的文本字段来输入可选值。

I'm new to zend framework but have made my first steps with it successfully.
Until now I have created some Zend_Forms which are mapping single records of my model
to the form fields. I have handled the forms with form classes for each case.
This works all very well until now.

Now I have the situation that I have to asign features to a product. Features and products are parts of my application. Features are stored in my database in three tables. For each feature there is one record in the third table.

  • First is the feature group where the name of the feature group is saved. Every feature should be asigned to a feature group.

  • Second table is the features table. This table has an foreign key to the feature group and the name of the feature.

  • Third table is some kind of many-to-many relation which connects features to products. This table has an aditional field which contains an optional value (beside the two foreign keys) for this unique feature of the product.

For example: if the product has a weight of 4,78 kg the value "4,78" is stored in the third table and the label "weight of %s kg" is stored in the second table. The feature group could be something like "physical attributes" had is saved in the first table.

To cut a long story short:
My problem is how to handle the case that I have to create and edit multiple database records in one form. The plan is to have a form with many checkboxes for each for a feature whereby features are thematicaly grouped. Every checkbox should have an aditional text field to input optional values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

眼眸印温柔 2024-08-29 01:48:00

您可以创建一个扩展 Zend_Form 的自定义表单类并将其用于您的类。
它可以接受模型的构造实例并基于该模型构造表单输入。
在控制器中进行表单验证后,您可以

$values = $form->getValues();

使用该数组再次填充模型

you could make a custom form class that extends Zend_Form and use that for you classes.
It could take in the construct instances of your models and construct the form inputs based on that models.
After form validation in your controller you can do

$values = $form->getValues();

and use that array to populate your models again

唔猫 2024-08-29 01:48:00

您可以尝试在表单类中创建子表单(Zend_Form_SubForm)。这可以分隔不同表的字段。对于编辑,在控制器中,当您从树表中提取所有数据时,您可以填充与表相对应的子表单。

You can try creating subforms (Zend_Form_SubForm) inside your form class. This can separate fields for different tables. For edition, in your controller, when you pull all the data from the tree tables, you can populate subforms that correspond to the tables.

月亮邮递员 2024-08-29 01:48:00

您可以尝试扩展 Zend_Form 来创建您自己的元素。

您将能够编写一个连接到数据库以获取属性(功能和产品)的类。

假设您编写了 My_Form_Element_Features & My_Form_Element_Products 类,您可以执行 $features = new My_Form_Features(); ,然后使用基类方法,例如 getValues()populate() 等。

您可以从那里开始:

--

要回答您的评论,您可以使用:

Zend_Form::setElementsBelongTo($array):

更多信息可以在< a href="http://framework.zend.com/manual/en/zend.form.advanced.html" rel="nofollow noreferrer">Zend_Form 高级手册页。

You can try to extend Zend_Form to create your own elements.

You will be able to write a class that connects to DB to get attributes (features & products).

Assuming you wrote My_Form_Element_Features & My_Form_Element_Products classes, you can do $features = new My_Form_Features(); and then use the base class methods like getValues(), populate(), etc.

You can take a look there to start :

--

To answer to your comment, you can use :

Zend_Form::setElementsBelongTo($array):

More information can be found at Zend_Form Advanced manual page.

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