Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:22:57+00:00 2026-06-18T05:22:57+00:00

I created a site for fun to learn CakePHP but for some reason I

  • 0

I created a site for fun to learn CakePHP but for some reason I can’t get a multiple select dropdown box to show my selected items. In this example 1 video game can have multiple difficulties (easy, normal, hard). On my Game edit page I have a multi select box to pick the difficulties. It shows all three difficulties and I can select them and it saves correctly. However when I return to the edit page the items I previously saved do not show highlighted as selected. I have verified that the records are saving correctly in the database.

Tables:
games
difficulties
difficulties_games

Models:

class Game extends AppModel {
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
    'Difficulty' =>
        array(
            'className' => 'Difficulty',
            'joinTable' => 'difficulties_games',
            'foreignKey' => 'game_id',
            'associationForeignKey' => 'difficulty_id',
            'unique' => 'true'
            )
);
}
class Difficulty extends AppModel {
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
    'Game' =>
        array(
            'className' => 'Game',
            'joinTable' => 'difficulties_games',
            'foreignKey' => 'difficulty_id',
            'associationForeignKey' => 'game_id',
            'unique' => 'true'
            )
);
}

Controller:

$game = $this->Game->findById($id);
$this->set('difficulties', $this->Game->Difficulty->find('list'));

View (edit.ctp):

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

This has to be something simple I am missing but I’ve read through the book on HABTM and searched here and couldn’t find much on multi-select boxes.

UPDATE:

Here is the entire edit function in the controller:

public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid post'));
    }

    $game = $this->Game->findById($id);
    if (!$game) {
        throw new NotFoundException(__('Invalid post'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
        $this->Game->id = $id;
        if ($this->Game->saveAll($this->request->data)) {
            $this->Session->setFlash('Your game has been updated.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash($this->Game->invalidFields());
        }
    }

    if (!$this->request->data) {
        $this->request->data = $game;
    }
    $this->set('systems', $this->Game->System->find('list'));
    $this->set('genres', $this->Game->Genre->find('list'));
    $this->set('difficulties', $this->Game->Difficulty->find('list'));


}

Also here is some more on the View:

echo $this->Form->create('Game');
echo $this->Form->input('name');
echo $this->Form->input('system_id');
echo $this->Form->input('genre_id');
echo $this->Form->input('Difficulty');
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Game');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:22:58+00:00Added an answer on June 18, 2026 at 5:22 am

    What CakePHP version are you using? Releases 2.2.6 and 2.3.0 have a bug related to showing existing habtm selected. So updated to 2.2.7 if using 2.2.6 or if using 2.3.0 use the master branch from github until the next bugfix release is done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a site where people can create a profile. But I am
I have created a site which has some roles. and each user can have
i created a site on my iis7 it's like http://localhost:88/my i also can access
I created a site using drupal 6.I want to show primary menu items only
I'm an amateur, admittedly so, but I'm having fun trying to learn a little
I created a site for my company, but it does not support all browsers.
I have created a site (can be accessed at http://dev.gkr33.com ) which is designed
I created a site with CakePHP responsible for displaying my CV. I would like
I have created a site using asp.net mvc3 and I have some action in
I have created a site with multiple languages in sitecore... I the content editor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.