2009-06-11 50 views
0

假设表格如下结构:CakePHP - 关系表中的相关模型,以下拉菜单显示?

注册参与者模型:

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

活动地点模型:

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

当我这样做,我认为: 回声$形式 - >输入( 'RegisteredParticipant.EventLocation.moderator');

它返回EventLocation.id s的下拉列表,而不是像我预期的EventLocation.moderators。任何想法可能是什么?

回答

0

Duh。 $this->RegisteredParticipant->EventLocation->find()没有使用'all',因为它是param。

0

您可以使用find('list')作为下拉菜单。 E.g:

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

你会得到类似的数组:

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

它可以在您的视图自动表单助手来处理。

1

没有人提到将$displayField添加到模型中。

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