cakexp协会问题在2x

I have two tables

  • messages

  • users

users has roles Doctor and user.

messages has doc_id & user_id.

How do I make an association, so that it returns me both data doc_id & user_id from users.

For the Role association on User you will need a join table, thats if i am right in believing a User can have many roles?

You'r Message model should only have a BelongsTo User, then when your doing loop ups you will use deep associations in your find methods to bring back some like...

// Find with deep assosiations
$this->Message->find('all', array());


Array
(
    [Message] => Array
        (
            [id] => 2
            [body] => example body...
        )

    [User] => Array
        (
            [username] => John
            [Role] => Array
                (
                    [0] => 2
                    [1] => 6
                )

        )

) 
class  Message extends AppModel {
  public $hasMany=array('MessageDetail');

    public $belongsTo = array(
                        'User'=>array(
                          'className'=>'User',
                          'foreignKey'=>'user_id',
                        ),
                      'Doctor'=>array(
                          'className'=>'User',
                          'foreignKey'=>'doc_id'
                        ),
                      'Petprofiles'=>array(
                          'className'=>'PetProfiles',
                          'foreignKey'=>'petprofiles_id'
                      )
                      );
}