views:

383

answers:

2

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.

But i am fairly new to Cake and not done much with associations before.

Thanks.

+2  A: 

in controller:

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

in view:

$form->input('category_id',array('type'=>'select'));
Aziz
This goes in the points_controller right? I added it there and it pops up a select box in the view but doesn't populate it...
joec
nevermind i sorted it ... had the first bit in the wrong action :)
joec
Might be worth updating your question with a bit more of your code, then. What Aziz is suggesting is the right way, assuming your `Point` model `belongsTo` your `Category` model.
Rob Wilkerson
+1  A: 

Also to generalize a bit

In a View with access to the Form helper

<?php
    echo $form->input( 'dataKey', array(
        'type' => 'select',
        'options' => array(
            'key1' => 'val1',
            'key2' => 'val2',
        ),
    ));
?>

The above will render a select input with 2 options. You can also place an empty option as the first item. Passing a value of true will simply append an empty option with a blank value to the beginning of the options rendered in the html.

<?php
    echo $form->input( 'dataKey', array(
        'type' => 'select',
        'options' => array(
            'key1' => 'val1',
            'key2' => 'val2',
        ),
        'empty' => true,
    ));
?>

You can pass a string to the 'empty' key to have it display custom text as the key field for the empty option.

<?php
    echo $form->input( 'dataKey', array(
        'type' => 'select',
        'options' => array(
            'California' => 'CA',
            'Oregon' => 'OR',
        ),
        'empty' => 'choose a state',
    ));
?>

One last example, you can also pre-select an option with the selected key. The value should match the value of one of the select options, not the key.

<?php
    echo $form->input( 'dataKey', array(
        'type' => 'select',
        'options' => array(
            'California' => 'CA',
            'Oregon' => 'OR',
        ),
        'empty' => 'choose a state',
        'selected' => 'California',
    ));
?>

From the Model Model->find( 'list', array( ... )); will always return an array formatted for use with select box options. If you pass data to your view stored in a variable with a lowercase plural model name IE ( $this->set( 'categories', $categories ); then you will automagically generate drop downs for related models by using the form helper in the view and passing it a data index of the same model name in singular form suffixed with "_id".

Aziz's answer at #2 is the example of that automagic kicking in.

Abba Bryant