2009-06-11 21 views
0

Compte tenu des structures de tableau suivant:CakePHP - Modèles associés dans une table de relations, affichés dans une liste déroulante?

modèle participant inscrit:

<?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') 
); 
} 
?> 

modèle Lieu de l'événement:

<?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') 
    ); 
} 
?> 

Quand je fais cela à mon avis: echo $ form-> input ('RegisteredParticipant.EventLocation.moderator');

Il renvoie une liste déroulante des EventLocation.id s, pas le EventLocation.moderators comme je m'y attendais. Des idées ce que ça pourrait être?

Répondre

0

Duh. $this->RegisteredParticipant->EventLocation->find() n'utilisait pas 'all' car c'est param.

0

Vous pouvez utiliser find ('list') pour les listes déroulantes. Par exemple:

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

Vous récupérerez un tableau comme:

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

manipulable sur votre point de vue automatiquement par l'assistant de formulaire.

1

personne n'a mentionné ajouter le $displayField au modèle comme ceci.

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') 
    ); 
} 
Questions connexes