Monday, December 28, 2009

How to auto-populate dependent habtm select boxes using ajax...

Hello,

I am new to CakePHP, and I am guessing that this has been answered
before, but I have a question about auto-populating dependent select
boxes.

I've been googling and looking for nearly a week, and I just can't
seem to figure this out! I've come close, but most of the examples
that I have found deal with a has_many relationship. I need to do this
with a habtm relationship.

So, here is my situation:

I have a Users model/table, with the fields: id, first_name,
last_name, company_id. I also have a Companies model/table, with the
fields: id, company_name. Each user can only belong to one company, at
a given time.

I also have a Contracts model/table, with the fields: id,
contract_details, is_accepted.

I have a join model/table for the users and the contracts that
contains the following fields: id, contract_id, user_id, is_client,
is_assigned. And, I have a companies and contracts join table: id,
company_id, contract_id.

A quick background on why it is designed this way: I can do work for a
given company numerous times. However, each time I work with that
company, I may be dealing with a different contact person in a
different physical location...but the bill still goes to the company.
Once the contract is created by me, and accepted by the client user I
need to assign the contract to an employee, and I may need to
communicate with other people to accomplish the goal of the contract,
and all of those people and communication need to be tied to that
contract. Thus the reason for the habtm between the contract-users and
contract-companies, and the need to specify who the client is, and who
is assigned. I hope that makes sense. I will be happy to clarify
further if I need to!

When I create a contract, I need to select a company from a drop down,
and then see a second drop down with a list of users in that company.
This is how I am selecting the client. I can't find any good info on
how to do this. I can find all kinds of info on how to do this with a
has_many relationship, and when I try those out (I've tried all of
them from <a href="http://www.devmoz.com/blog/2007/04/04/cakephp-
update-a-select-box-using-ajax/
">devMoz</a>, to <a href="http://
www.endyourif.com/ajax-select-box-in-cakephp/">End Your If</a>, to <a
href="http://www.jamesfairhurst.co.uk/posts/view/
using_ajax_to_populate_a_select_box_in_cakephp/
>James Fairhurst</a>,
all the way to something I found on <a href="http://
forum.phpsitesolutions.com/php-frameworks/cakephp/ajax-cakephp-
dynamically-populate-html-select-dropdown-box-t29.html
">
phpsitesolutions</a>), everything looks and works great, until I try
to save the contract. Then I discover that my company and user data is
not being saved to the database, even though it is displaying
correctly on the form.

I am at a loss. I am willing to take a different approach, but I'm not
sure what that would even look like. If someone can point me in the
right direction, that would be great!

Thanks,

Jeremiah

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: