2017-05-30 7 views
0

私はフォローしているユーザー名を取得しようとしている "フォローシステム"(ツイッターのような)を構築しています。しかし、私はfollower_idまたはfollow_idだけを得ることができます。自己恋愛のユーザ名を取得する - Cakephp 3

//UsersFollowersTable.php

public function initialize(array $config) 
{ 
    parent::initialize($config); 

    $this->setTable('users_followers'); 

    $this->addBehavior('Timestamp'); 

    $this->belongsTo('Followeds', [ //who i'm following 
     'className' => 'Users', 
     'unique' => true, 
     'foreignKey' => 'follower_id', 
     'targetForeignKey' => 'Users.id', 
     'joinType' => 'INNER' 
    ]); 
    $this->belongsTo('Followers', [ //my followers 
     'className' => 'Users', 
     'unique' => true, 
     'foreignKey' => 'followed_id', 
     'targetForeignKey' => 'Users.id', 
     'joinType' => 'INNER' 
    ]); 
} 

//UsersTable.php

パブリック関数の初期化(配列$設定) { 親::初期化($の設定):これは私が得たものです;

$this->hasMany('Followed', [ 
     'className' => 'UsersFollowers', 
     //'joinTable' => 'users_followers', 
     'unique' => true, 
     'foreignKey' => 'follower_id', 
     'targetForeignKey' => 'id' 
    ]); 

     $this->hasMany('Follower', [ 
     'className' => 'UsersFollowers', 
     //'joinTable' => 'users_followers', 
     'unique' => true, 
     'foreignKey' => 'followed_id', 
     'targetForeignKey' => 'id' 
    ]); 


} 

どのように$ user-> "Username"を得ることができますか? //がUserController パブリック関数ビュー($のID = NULL)

+0

'debug($ user-> excute());'を使って結果を確認しましたか? – chrisShick

答えて

0

ユーザー多く続いた {

$user = $this->Users->get($id, [ 
     'contain' => ['Collections', 'Images', 'Items', 'Likes', 'Messages', 'Profiles', 'Ratings', 'Followed', 'Follower' ] 
    ]); 

}

おかげで、そう$user->followedそれはループ、Collectionある:

if ($user->has('followed')) { 
    foreach ($user->followed as $followed) { 
     debug($followed->username); 
    } 
} 
+0

私はユーザー名を取得できません。見てください: – Henricristo