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…
When adding an input for
field_id, the form helper looks for a variable called$fields. I.e., the name without_idand pluralized.$parentCategoriesdoes not fit that description, so it’s not used.$parentswould be.Second,
$this->Category->ParentCategoryis the same as$this->Category. Both reference the Category model. No need to go throughParentCategory.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
recursivesettings. You should instead make it a proper Tree and use the TreeBehavior methods to query it.