gpt4 book ai didi

Cakephp HABTM : View generating drop down instead of multi value selectbox

转载 作者:行者123 更新时间:2023-12-02 22:04:16 27 4
gpt4 key购买 nike

我正在尝试处理个人资料和资格表之间的 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'));
}
}

查看:编辑.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 = $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 ")

食谱中的片段:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com