Search code examples
cakephpformhelper

Creating 'select' listboxes using FormHelper in CakePHP


I have two models, Category and Point. The associations are defined as:

Category hasMany Point
Point belongsTo Category

I would like, when adding Points to my database, to be able to select the category it belongs to from a <select> box, along with the rest of the form data.

Where would I need to set the category list and how could I do it? And how would I produce the select box?

I assume it could be done with

$form->input('categorieslist',array('type'=>'select')); //categorieslist needs
                                                        //setting somewhere.

Solution

  • In the controller:

    $categories = $this->Point->Category->find('list');
    $this->set(compact('categories'));
    

    In the view:

    $form->input('category_id',array('type'=>'select'));