-1

I have three models

  1. User
  2. Status
  3. Comments

I am done with User and Status associations now i want to make join for comments but i am a bit confused and i think it would be like this...

Scenario 1: A user hasMany status and status belongsTo user [ DONE ] Scenario 2: A status hasMany comments and comments belongsTo status [ Which will be done as well ]

The thing which is eating my mind is that comments also belongsTo user [ Right or Wrong ? ]

If right then is this the way i am gonna use belongsTo

var $belongsTo = array('status','user');

and what about find('all') ? would it be like this

$this->User->Status->Comment->find('all'); 
4

1 回答 1

1

Ok, based on application that looks like Facebook, I think you should have these associations like:

User

public $hasMany = array(
    'Status' => array(
        'className' => 'Status',
        'foreignKey' => 'user_id',
        'dependent' => true
    ),
    'Comment' => array(
        'className' => 'Comment',
        'foreignKey' => 'user_id',
        'dependent' => true
    ),
);

Status

public $hasMany = array(
    'Comment' => array(
        'className' => 'Comment',
        'foreignKey' => 'status_id',
        'dependent' => true
    ),
);

public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id'
    ),
);

Comment

public $belongsTo = array(
    'Status' => array(
        'className' => 'Status',
        'foreignKey' => 'status_id'
    ),
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id'
    ),
);

And to find comments of a status, for example, you can use Containable Behavior.

Hope this helps.

于 2012-10-09T11:40:03.553 回答