开发者

CakePHP 1.3, Form Helper and Self Joined Table

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 _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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜