views:

635

answers:

3

Hi everyone,

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.

A: 

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.

Boris Guéry
I have connected my form class to db. But at the moment I'm unsure how to build the form from the array I get from the db. I tried it with subforms but I'm not able to get a field name like productfeature[groupid][featureid][fieldname] ...
sober
Thanks for the hint about Zend_Form::setElementsBelongTo($array);. But the manual doesn't describe how to use this Method. Do you know a good tutorial about that ? Thanks in advance.
sober
A: 

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

solomongaby
A: 

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.

Marcin
Maybe subforms are the right way to solve this. But how can I create a subform strukture from a two dimensional array ?
sober