Search code examples
phpcakephpbelongs-to

Optional belongsTo relationship in cakephp


I have a simple Category model in my CakePHP application. I want to add sub-categories, and do this by simply adding a parent_id column.

The parent_id is a belongsTo relationship, that references back to the same Category model.

When I generate my admin scaffolds, a dropdown will shop up (correct!), but I'd like to include a NULL option in this dropdown (for top-level categories).

Is this possible? And if so, how..

(Note: not interested in Tree behaviour right now)


Solution

  • Sounds like you may be looking for the empty option:

    <?php echo $form->input( 
      'field', 
      array( 'options' => array( 1, 2, 3, 4, 5), 
      'empty' => 'Select one' 
    ); ?>
    

    http://book.cakephp.org/view/201/options-empty