yossi
yossi

Reputation: 3164

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?

Upvotes: 0

Views: 202

Answers (2)

yossi
yossi

Reputation: 3164

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.

Upvotes: 0

user470714
user470714

Reputation: 2888

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

Upvotes: 1

Related Questions