我正在尝试使用配置文件和资格表之间的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'));
}
}
Vew: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>
这样生成的编辑视图包含下拉列表,一次为Qualifications属性选择一个值。
我想知道如何使用多值选择框为资格生成视图? 而且,我的代码现在的错误是什么?
答案 0 :(得分:3)
第一次发布海报,长时间用户。
我今天偶然发现了这个问题,并最终使用了后续解决方案,这确实非常有效。但是,我离开了自己,想知道“为什么CakePHP在HABTM上的关系不合适呢?”特别考虑(至少在我的情况下)大多数文件已经在蛋糕控制台中烘烤。
如果我们深入探讨这个问题,我们发现这个问题实际上非常简单。仔细查看PostsController.php
中的这个片段,可以看出Cake如何在函数的HABTM关系中构建,并使用$this->set()
将其传递给视图(重要:使用小写复数版本“称呼“):
$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,可以用单数形式指定变量。标题案例(即:“称呼”)
烹饪书中的片段:
假设User hasAndBelongsToMany Group。在您的控制器中,使用选择选项设置camelCase复数变量(在这种情况下为group - &gt; groups,或ExtraFunkyModel - &gt; extraFunkyModels)。在控制器操作中,您将添加以下内容:
$this->set('groups', $this->User->Group->find('list'));
在视图中,可以使用以下简单代码创建多重选择:
echo $this->Form->input('Group');
如果没有任何必要的字段调整,应解决您的问题。
干杯!
烘烤。
答案 1 :(得分:2)
也许您需要进一步配置输入:
echo $this->Form->input('Qualification',array(
'label' => 'Qualifications',
'type' => 'select',
'multiple' => true, // or 'checkbox' if you want a set of checkboxes
'options' => $qualifications,
'selected' => $html->value('Qualification.Qualification'),
));
每当我遇到HABTM协会时,我都会使用this博文。在我看来,默认情况下,选择输入可能需要一组复选框 - 也许具有更高CakePHP洞察力的人可以在这里进行编辑?
答案 2 :(得分:0)
更改
echo $this->Form->input('qualification');
到
echo $this->Form->input('qualification', array(
'multiple' => true
));
CakePHP手册提供了有关表单助手input options的更多信息。