views:

20

answers:

1

I have a few models which are not in the default cakephp format

user(id, name....)

1 Harsha ...
2 John ....

dishes(id, name, price ...)

1 "Cheese Pizza" 6
2 "Zinger Burger" 3

restaurants (id, name, .....)

1 "KFC" ...
2 "Pizza Hut" ...

module(id, name) values (User, Dishes, Restaurants)

1 "Users"
2 "Dishes"
3 "Restaurant"

items (id, module_id, item_id)

1 1 1 (refers to User Harsha)

2 3 2 (refers to Pizza hut)

3 2 2 (refers to Zinger Burger)

4 1 2 (refers to User John)

where item_id refers to the Id of Users, Dishes or Rests Depending on the module_id


reviews (id, parent_id, review, time, item_id, commenter_id)

1 0 "Best Burger in the world" "time" 3 1 (refers to Harsha reviewing Zinger Burger)

2 1 "Yes i love Zingers tooo" time 3 2 ( refers to John replying to Harsha's Review)

I am a little messged up on how to draw up the relationships in cakephp

+1  A: 

In the book at this page: http://book.cakephp.org/view/1039/Associations-Linking-Models-Together you'll find a guide to the possible keys you can set on the relationship, e.g.

foreignKey: the name of the foreign key found in the other model. This is especially handy if you need to define multiple hasOne relationships. The default value for this key is the underscored, singular name of the current model, suffixed with ‘_id’. In the example above it would default to 'user_id'.

Assuming reviews and items are children in their associations, for both ends of the relationships, you'd set the foreignKey as 'item_id'.

Something like:

dishes:
class Dish extends AppModel {
    var $name = 'Dish'; 
    var $hasMany = array(
        'Item' => array(
            'className'     => 'Item',
            'foreignKey'    => 'item_id',
            ...
items:
class Item extends AppModel {
    var $name = 'Item'; 
    var $belongsTo = array(
        'Dish' => array(
            'className'     => 'Dish',
            'foreignKey'    => 'item_id',
            ...
            ),

        'Restaurant' => array(
            'className'     => 'Restaurant',
            'foreignKey'    => 'item_id',
            ...

It's difficult to be more precise, but you haven't provided any datamodel. To handle the selection of Model via the Module, you'll need to write some code on a model somewhere, which one(s) depends on how you access the data.

However, it looks to me like a good time to restructure the database!

Leo
the confusion is Item belongs to say User. so belongsTo should be like this http://pastebin.com/uVrbzVQi and not to Item right ?
Harsha M V

related questions