2013-05-04 45 views
2

我正在嘗試使用Profile和Qualifications表之間的HABTM關聯。Cakephp 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', 
    ) 
); 
} 

控制器: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> 

因此生成的編輯視圖包含下拉菜單,以便一次爲「資格」屬性選擇單個值。

我想知道如何生成一個視圖與多值選擇框的資格? 此外,我現在的代碼中有什麼錯誤?

回答

0

變化

echo $this->Form->input('qualification'); 

echo $this->Form->input('qualification', array(
    'multiple' => true 
)); 

CakePHP的手動對錶單輔助input options更多信息。

+0

是的!這對我有效。現在獲取正確的選擇框......但爲什麼我需要手動執行它,因爲這應該自動工作? – i01000001 2013-05-05 07:38:15

+0

我認爲你是對的,但我不知道如何。這些選項只是一個數組,其中的ID爲鍵,名稱爲數值,也可以是屬於關係的數組。 – 2013-05-05 12:40:35

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'), 
)); 

我用this博客文章時,我碰到HABTM關聯。在我看來,默認情況下,一組複選框可能需要通過選擇輸入 - 也許有更多CakePHP洞察力的人可以在此嵌套?

+0

感謝您的回覆!我現在可以看到多選框。 出於某種原因,您提到的更改不起作用,然後我將'$ form->'更改爲'$ this-> Form->',它使界面返回而不是錯誤。我認爲你的代碼是Cake的一些早期版本。我正在使用2.2.3 – i01000001 2013-05-05 07:36:37

+0

感謝您的反饋@ i01000001,我已經爲未來的觀衆更新了我的答案 – 2013-05-05 09:34:43

3

第一次張貼海報,長時間用戶。

今天我偶然發現了這個問題,並最終使用了後來的解決方案,它的確工作得很好。但是,我留下了自己的疑惑:「爲什麼CakePHP不能正確地獲取HABTM關係?」特別是考慮(至少在我的情況下)大多數文件已在蛋糕控制檯中烘焙。

如果我們深入探討這個問題,我們會發現問題其實很簡單。在這個片段在PostsController.php仔細觀察,可以發現蛋糕如何建立在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')); 

按照蛋糕烹調手冊中,爲了使用表單輔助時利用這個HABTM的在前端處於單數形式&標題情況指定變量(即:「稱謂」)

烹飪書中的片段:

假設用戶擁有和擁有許多組。在您的控制器中,使用select選項設置camelCase複數變量(在這種情況下爲組 - >組,或ExtraFunkyModel - > extraFunkyModels)。在控制器動作,您會把下列內容:

$this->set('groups', $this->User->Group->find('list')); 

而且多選擇可以用這個簡單的代碼來創建視圖:

echo $this->Form->input('Group'); 

應該能夠解決您的問題,沒有任何必要的實地扭捏。

乾杯!

烘烤。