Thursday, November 29, 2012

Re: Completely Stuck trying to list names of Contacts class

I assume you want to populate a field called 'contact_id' in your clients table? If so...

Your Client model should have a belongsTo association with the Contact model:

var $belongsTo = array(
'Contact'
);

In the add (and also edit) action of your clients_controller:

$contacts = $this->Client->Contact->find('list');  // note that it's daisy chaining the models along the associations

Note: if the contacts table contains a field called 'client_id' so that each contact can only belong to one client, you'll also need to link the Contact model with the Client model:

var $belongsTo = array(
'Client'
);

Then you'll also need to add a constraint to your find so that you only brings back contacts for 'this' client:

$contacts = $this->Client->Contact->find(
'list',
array(
'conditions' => array(
'Contact.client_id' => $clientId  // where $clientId is 'this' client
)
)
);

the $contacts variable is now an array populated with the matching contacts with two keys; 'id' => 'name'/'title' depending on what you've called it.

At the end of the add/edit action make sure you are sending the $contacts variable through to the view:

$this->set('contacts, $contacts);

This will make the $contacts variable available in the view. If you have more than one variable to pass through you can either repeat the set command for each, or do this (which is preferable):

$this->set(compact(
'contacts',
'otherVariable',
'etc'
));

This will hunt for variables called $contacts, $otherVariable and so on and make view variables from them if they exist.

In your form, have an input called 'Client.contact_id'. The Cake form helper will automatically look for a variable called $contacts (it's knocked off the '_id' and looked for a variable that matches the plural). If it finds one it will by default render the input as a select list populated with your list of contacts. Also by default it will select the first option. To stop it doing that, do this:

echo $this->Form->input('Client.contact_id', array('empty' => true));

Hope that gets you started.


On 28 Nov 2012, at 20:22, Andrew Cook <ais4ocho@gmail.com> wrote:

I just don't even kind of understand how to access my data..  I went through the blog tutorial and that was all fine, but for my application all I'm wanting to do is this:

I want to create a new Client via a form, which I've created.. in a dropdown list box I want to list all of the names in the Contact table.... I can't figure out how to do it at all...  I am just not getting how things are related I guess..



--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
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.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: