我在舞者和舞者之间有 HABTM 关系。事情似乎按预期工作,除了在 edit.ctp 上编辑我的 Dances 时,CakePHP 显示的是一个下拉框,而不是我期望的多选框。对我来说疯狂的是,多选框与 edit.ctp 以另一种方式工作,用于编辑舞者!我不明白为什么它会以一种方式工作,但不能以另一种方式工作。
这是我定义舞蹈和舞者之间关系的舞蹈模型:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Dancer' => array(
'className' => 'Dancer',
'joinTable' => 'dancers_dances',
'foreignKey' => 'dance_id',
'associationForeignKey' => 'dancer_id',
'unique' => 'keepExisting',
)
);
这是我的舞蹈控制器:
public function edit($id = null) {
if (!$this->Dance->exists($id)) {
throw new NotFoundException(__('Invalid dance'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Dance->save($this->request->data)) {
$this->Session->setFlash(__('The dance has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The dance could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Dance.' . $this->Dance->primaryKey => $id));
$this->request->data = $this->Dance->find('first', $options);
}
$users = $this->Dance->User->find('list');
$dancers = $this->Dance->Dancer->find('list');
$this->set(compact('users', 'dancers'));
}
这是我的舞蹈视图的 edit.ctp
<div class="dances form">
<?php echo $this->Form->create('Dance'); ?>
<fieldset>
<legend><?php echo __('Edit Dance'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('dance_name');
echo $this->Form->input('users_id');
echo $this->Form->input('Dancers');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Dance.id')), null, __('Are you sure you want to delete # %s?', $this->Form->value('Dance.id'))); ?></li>
<li><?php echo $this->Html->link(__('List Dances'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Dancers'), array('controller' => 'dancers', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Dancers'), array('controller' => 'dancers', 'action' => 'add')); ?> </li>
</ul>
</div>
我认为相关的表格是:dancers, dancers_dances, dances
在 dancers_dances 表上,我有以下列: id, dancer_id, dance_id
让我知道更多信息是否会有所帮助。
提前致谢。