开发者

cakePhp adding record problem

I have 3 tables:

item {name,id,category_id}

category {id,name,section_id}

sections {id,name}

As you can see, each item is related to a category, and each category is related to a section. I used the 开发者_运维知识库bake option and got it all baked, well, not all.. since the Category.name can be duplicated, and since it is the displayed field, I find my self in a situation which prevent me of knowing which category I really use.

Any ideas?


You could do something like this. Add to your Category model:

    var $virtualFields = array(
       'category_unique_name' => 'CONCAT(Category.name, "-", Category.id)'
    );

    var $displayField = 'category_unique_name';

This virtual field you can use to tell which category is which based on the combination of its id and name. There are some limitations to virtual fields, however. Read about it here: http://book.cakephp.org/view/1608/Virtual-fields#!/view/1608/Virtual-fields


Well, the answer was: Using find('list'), with recursive option ON, and with 3 fields instead of two. That way it grouped the categories under the sections.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜