嘗試加載用戶以「Cakey」方式關注的所有帖子。CakePHP 3 - 加載用戶正在關注的用戶的帖子
三個表:
用戶(ID,用戶名,密碼)
帖子(ID,文字,USER_ID,創建)
如下(ID,USER_ID,following_id )
我認爲我的關係設置正確,因此我可以加載用戶關注者和人員以下這些:
用戶表
$this->belongsToMany('Following', [
'className' => 'Users',
'foreignKey' => 'user_id',
'targetForeignKey' => 'following_id',
'joinTable' => 'follows'
]);
$this->belongsToMany('Followers', [
'className' => 'Users',
'foreignKey' => 'following_id',
'targetForeignKey' => 'user_id',
'joinTable' => 'follows'
]);
遵循表
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
帖子表
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
正掙扎着選擇從單個用戶遵循用戶的所有職務。認爲我需要使用這裏記錄的matching()
方法:Filtering by Associated Data Via Matching And Joins,但似乎沒有得到正確的結果。
試圖沿着這些路線的東西:
$user_id = $this->Auth->user('id');
$posts = $this->Posts->find()->matching('Users.Following', function ($q) use ($user_id) {
return $q->where(['Users.id' => $user_id]);
})->all();
UPDATE:
我想我有我的關係設置正確,這個發現將返回用戶,以及所有的用戶如下:
$users = $this->Posts->Users->get(1, [
'contain' => ['Following']
]);
將您的find()查詢放在這裏 –
已添加到問題中。我想我正沿着正確的路線走...... –
你得到的結果是什麼? –