views:

35

answers:

1

Hi everyone,

My Self Joined Categories table is as follows:

id, name, description, parent_id

I used Cake Bake to generate the Model, Controller and Views. Model has the $belongsTo and $hasMany association set up. In add() of the controller,

$parentCategories = $this->Category->ParentCategory->find('list');
$this->set(compact('parentCategories'));

is present. In the add view, the cake bake generated form is:

<?php
        echo $this->Form->input('name');
        echo $this->Form->input('description');
        echo $this->Form->input('isincome');
        echo $this->Form->input('parent_id');
?>

When I run in browser, the parent_id field is getting a drop down, but it is not being filled with any data. I used

<?debug($parentCategories);?> 

in the add view, and it happily outputs

Array
(
    [1] => Entertainment
    [2] => Groceries
)

But this array is not being used for filling that drop down by the Form helper. What should I do? Is this a bug with Cake's Form helper in 1.3? It never occurred in 1.2...

+1  A: 

When adding an input for field_id, the form helper looks for a variable called $fields. I.e., the name without _id and pluralized. $parentCategories does not fit that description, so it's not used. $parents would be.

Second, $this->Category->ParentCategory is the same as $this->Category. Both reference the Category model. No need to go through ParentCategory.

Third, it's not usually a good idea to join a Tree model to itself. You'll understand why when you start to query with higher recursive settings. You should instead make it a proper Tree and use the TreeBehavior methods to query it.

deceze
Thanks deceze, it worked! But I wonder why bake generated such output then.. What ever I posted above is cake bake generated code!
Ashok
@Asok That's why you don't self-join a model, bake doesn't handle it very gracefully. :)
deceze