我正在尝试使用个人资料和资格表之间的 HABTM 关联。
模型:Profile.php
App::uses('AppModel', 'Model');
class Profile extends AppModel {
public $hasAndBelongsToMany = array(
'Qualification' => array(
'className' => 'Qualification',
'joinTable' => 'profile_qualifications',
'foreignKey' => 'profile_id',
'associationForeignKey' => 'qualification_id',
'unique' => 'keepExisting'
)
);
}
模型:Qualification.php
App::uses('AppModel', 'Model');
class Qualification extends AppModel {
public $hasAndBelongsToMany = array(
'Profile' => array(
'className' => 'Profile',
'joinTable' => 'profile_qualifications',
'foreignKey' => 'qualification_id',
'associationForeignKey' => 'profile_id',
'unique' => 'keepExisting',
)
);
}
Controller :ProfilesController.php
App::uses('AppController', 'Controller');
class ProfilesController extends AppController {
public function edit() {
$this->Profile->id = $this->Auth->user('profile_id');
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Profile->save($this->request->data)) {
$this->Session->setFlash(__('The profile has been saved'));
$this->redirect(array('action' => 'view'));
} else {
$this->Session->setFlash(__('The profile could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Profile->read(null, $this->Auth->user('profile_id'));
}
$salutations = $this->Profile->Salutation->find('list', array('fields' => array('Salutation.id', 'Salutation.abbr_name')));
$qualifications = $this->Profile->Qualification->find('list', array('fields' => array('Qualification.id', 'Qualification.abbr_name')));
$this->set(compact('salutations', 'qualifications'));
}
}
查看:edit.ctp
<div class="profiles form">
<?php echo $this->Form->create('Profile'); ?>
<fieldset>
<legend><?php echo __('My Profile'); ?></legend>
<?php
echo $this->Form->input('salutation_id');
echo $this->Form->input('first_name');
echo $this->Form->input('middle_name');
echo $this->Form->input('last_name');
echo $this->Form->input('qualification'); /* gives drop down not multi select */
echo $this->Form->input('bio');
echo $this->Form->input('email');
echo $this->Form->input('mobile');
echo $this->Form->input('phone');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
由此生成的编辑 View 包含下拉列表,用于一次为“资格”属性选择一个值。
我想知道如何生成具有多值选择框的资格 View ? 此外,我的代码现在有什么错误?
最佳答案
第一次发帖,长期用户。
我今天偶然发现了这个问题,并最终使用了后续的解决方案,该解决方案确实工作得很好。然而,我让自己想知道“为什么 CakePHP 不能正确拾取 HABTM 关系?”特别是考虑到(至少在我的情况下)大多数文件都是在蛋糕控制台中烘焙的。
如果我们深入研究这个问题,我们会发现问题其实很简单。仔细观察 PostsController.php
中的这段代码可以揭示 Cake 如何构建 HABTM 与函数的关系,并使用 $this->set()
来传递它到 View (重要:使用小写复数版本“salutations”):
$salutations = $this->Profile->Salutation->find('list', array('fields' => array('Salutation.id', 'Salutation.abbr_name')));
$qualifications = $this->Profile->Qualification->find('list', array('fields' => array('Qualification.id', 'Qualification.abbr_name')));
$this->set(compact('salutations', 'qualifications'));
根据 Cake Cook Book,为了在前端使用表单助手时利用此 HABTM,需要以单数形式和标题大小写 指定变量(即:“Salutation ”)
cooking 书中的片段:
Assuming that User hasAndBelongsToMany Group. In your controller, set a camelCase plural variable (group -> groups in this case, or ExtraFunkyModel -> extraFunkyModels) with the select options. In the controller action you would put the following:
$this->set('groups', $this->User->Group->find('list'));
And in the view a multiple select can be created with this simple code:
echo $this->Form->input('Group');
无需进行任何必要的现场调整即可解决您的问题。
干杯!
烘烤。
关于Cakephp HABTM : View generating drop down instead of multi value selectbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374711/