私のアプリには3つのテーブルがあります。
Users
Profiles
Friends
ユーザーには1つのプロファイルがあり、プロファイルには1人のユーザーがいて、多くの友達がいます。友達にはたくさんのプロフィールがあります。
プロファイルモデルには、プロファイルのプロファイルのリストを取得するメソッドがあります(ただし、ユーザーテーブルから取得した関連するユーザー名を使用します)。
public function getFriends($username) {
return $this->find('all', array(
'conditions' => array(
'User.username' => $username,
'Friend.status' => 1
)
));
}
したがって、ユーザー名がユーザーと一致し、友人のステータスが1である場合に一致するプロファイルのリストを取得する必要があります。これを行うにはどうすればよいですか?
DBを理解できるように、関連付けも投稿します。
ユーザーモデル:
public $hasOne = 'Profile';
プロファイルモデル:
public $belongsTo = 'User';
public $hasMany = array(
'ProfileFrom'=>array(
'className'=>'Friend',
'foreignKey'=>'profile_from'
),
'ProfileTo'=>array(
'className'=>'Friend',
'foreignKey'=>'profile_to'
)
);
public $hasAndBelongsToMany = array(
'Friendship' => array(
'className' => 'Profile',
'joinTable' => 'friends',
'foreignKey' => 'profile_from',
'associationForeignKey' => 'profile_to'
)
);
public $actsAs = array('Containable');
フレンドモデル:
public $belongsTo = array(
'UserFrom'=>array(
'className'=>'Profile',
'foreignKey'=>'profile_from'
),
'UserTo'=>array(
'className'=>'Profile',
'foreignKey'=>'profile_to'
)
);
public $actsAs = array('Containable');