views:

305

answers:

1

Hello,

I am using CakePHP 1.2. I have a person model that hasMany 'Document'. When I edit a document, the select box for the owning person appears (echo $form->input('person') where person has been defined in the documents_controller like this:

$allPeople = $this->Document->Person->find('list', array('fields' => array('first_name')));
$this->set('people', $allPeople);

When I edit a document's record, I want the person owning the document to be selected and displayed in the box. Right now, the app just makes the list box but doesn't highlight the correct owner (though the DB has the person's id).

Thank you, Frank Luke

+3  A: 

Hi Frank,

In your edit view, you should add an extra parameter to your $form->select(), called $selected. This way, you can specify which item should be selected from the list.

Example (just an example, you should rewrite it for your own situation):

<?php echo $form->select('Document.person', $allPeople, $this->data['Document']['Person']['id']); ?>

More information:
http://book.cakephp.org/view/728/select

-- Bjorn

Bjorn
You can also use `$form->input('Document.person', array('options' => $people, 'selected' => $this->data['Document']['Person']['id']));` http://book.cakephp.org/view/199/options-selected
deizel