tags:

views:

388

answers:

3

Given the following table structures:

Registered Participant model:

<?php
class RegisteredParticipant extends AppModel {
var $name = "RegisteredParticipant";
var $primaryKey = "id";
var $belongsTo = array(
  'EventLocation' => array('className' => 'EventLocation'),
  'RegistrationStatus' => array('className' => 'RegistrationStatus'),
  'Specialty' => array('className' => 'Specialty')
);
var $hasMany = array(
  'DietaryRestriction' => array('className' => 'DietaryRestriction')
);
}
?>

Event Location model:

<?php
class EventLocation extends AppModel {
   var $name = 'EventLocation';
   var $primaryKey = 'id';
   var $belongsTo = array(
      'Event' => array('className' => 'Event', 'foreignKey' => 'event_id'),
      'Location' => array('className' => 'Location', 'foreignKey' => 'location_id')
   );
}
?>

When I do this in my view: echo $form->input('RegisteredParticipant.EventLocation.moderator');

It returns a dropdown list of the EventLocation.ids, not the EventLocation.moderators like I expected. Any ideas what it could be?

A: 

Duh. $this->RegisteredParticipant->EventLocation->find() wasn't using 'all' as it's param.

leo
A: 

You can use find('list') for dropdowns. E.g:

$locations = $this->RegisteredParticipant->EventLocation->find('list', array(
    'fields' => array('some_fields')
));
$this->set('locations', $locations);

You will get back an array like:

array(
    'id_1' => 'some_field_contents',
    'id_2' => 'some_field_contents',
    'id_3' => 'some_field_contents'
);

Which can be handled on your view automatically by the Form helper.

Sander Marechal
A: 

nobody mentioned adding the $displayField to the model like this.

class EventLocation extends AppModel {
    var $name = 'EventLocation';
    var $displayField = 'moderators';
    var $primaryKey = 'id';
    var $belongsTo = array(
        'Event' => array('className' => 'Event', 'foreignKey' => 'event_id'),
        'Location' => array('className' => 'Location', 'foreignKey' => 'location_id')
    );
}
naterkane