Zend Framework single page form dealing with many to many relationships
I have a database that has a scenario where there is a many to many relationship between two tables (with a third 'associative' table between them):
Eg:
Table 1 : user (primary key id)
Table 2 : contact_info (primary key id)
Associative table: user_has_contact_info (primary key id, foreign keys: user_id and contact_info_id)
Now the requirements for the application are to be able to add multiple 'contact_info' entries on the same page as c开发者_如何学JAVAreating a user record:
Eg: (Form)
UserName: ___________
Contact Type Contact Detail
Phone 123-4567
Email me@here.com
_________V _______________ [Add Contact Button]
... plus more fields for the user table ...
So in my example above, I have the username field (which would go in the user table), a table of all contact information records that relate to the user (in the username field), that would go into the contact_info table, and then the form elements for contact_type and contact_details (under their respective columns below the table) (with an add button?) which would add the contact information to the contact 'table'. Then below all that, more fields that belong to the user table.
If you are following me so far, the problem I am facing is the client wants this to show on a single form not multi page or separated in two forms, the flow must 'look' like a single form (even though the elements will belong to different database tables.
Second, it is simple enough to have the data put into the contact_info table, however there is no actual user_id generated until the rest of the 'user' fields are filled out and the user record is updated, and therefore, I am failing to see how to add the associative record in user_has_contact_info, without saving the user record first and adding the contact info second.
Finally, the preference is to avoid javascript (though not completely ruled out, because pretty much any solution is going to require either javascript -or- page refreshes with partial form field data retention and possibly even validation and filter issues -- have not gotten that far yet)
So my ultimate question is, what is the best way to handle many-to-many database table relationships using Zend_Form in the scenario above. I might be overthinking this problem and missing the simple solution here, and look forward to different perspectives.
Please note, this is not a question about how to design the database, rather how to create the zend form to handle the database the way it is designed.
There seem to be two issues here:
Inserting into the user table and into the contact table and into the user-contact table.
Dealing with dynamic - that is, client-side-determined - number of contact records per user.
For the second part, see Jeremy Kendall's excellent article on dealing with a client-side-determined number of fields with Zend_Form
. The upshot there is client-side javascript that tracks the number of new fields paired with a preValidation()
method on the form that reads this info and modifies the form object before you call isValid()
and getValue()
.
For the first part, it seems like you essentially have it right.:
Create the user record and get the user id.
For each contact block in your form, create the contact record and get the contact id; and create the associated user-contact record with the two id's jammed in.
Might want to wrap some of it up in a transaction so that if one part fails - say, failure of the user-contact insertion - you don't end up with an orphan contact record unconnected to any user.
精彩评论