CakePHP - 关系表中的相关模型,显示在下拉列表中?

时间:2009-06-11 15:29:23

标签: php 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')
   );
}
?>

当我在我的观点中这样做时:      echo $ form-&gt; input('registeredParticipant.EventLocation.moderator');

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

3 个答案:

答案 0 :(得分: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')
    );
}

答案 1 :(得分:0)

咄。 $this->RegisteredParticipant->EventLocation->find()没有使用'all'作为它的参数。

答案 2 :(得分: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'
);

可以通过表单助手自动处理您的视图。