我需要設置關注者,關注(myFriends)hasFriend logic到我的項目。列「odobera」意思是「跟隨」示例nick(id用戶)odobera(跟隨該用戶)。用戶(25)跟隨用戶(37)。Symfony主義實體朋友有朋友關注者
請求表:
User entity:
/**
* @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="odobera")
*/
protected $followers;
/**
* @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="nick")
*/
protected $myFriends;
public function __construct()
{
parent::__construct();
$this->followers = new \Doctrine\Common\Collections\ArrayCollection();
$this->myFriends = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get myFriends
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMyFriends()
{
return $this->myFriends;
}
/**
*
* @param \TB\UserBundle\Entity\User $user
* @return bool
*/
public function hasFriend(User $user)
{
return $this->myFriends->contains($user);
}
class Requests
{
/**
* @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User", inversedBy="myFriends")
* @ORM\JoinColumn(name="nick", referencedColumnName="id")
*/
protected $nick;
/**
* @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User",inversedBy="followers")
* @ORM\JoinColumn(name="odobera", referencedColumnName="id")
*/
protected $odobera;
在控制器:
$myFollowers=$user->getMyFriends();
回報:
什麼是好的:返回1條記錄。我實際上遵循只是一個人,你可以在這裏看到記錄的ID是24395
DB請求表:
我不知道,如果是很好的getMyFriends函數返回的響應在「格式」。請仔細看一下。
然後,我有選擇的追隨者從查詢和循環:
{% for follower in followers %}
and i print data like this (works greate) {{ follower.nick }}
or if i want some fields from user entity {{ follower.nick.rank }}
{% endfor %}
但問題就在這裏:
{% if (app.user.hasFriend(follower.nick)) %}
返回假的,爲什麼呢?我跟着這個用戶,因爲我用轉儲檢查控制器:P幾行。
是什麼'{{轉儲(app.user)}}'呈現? – cheesemacfly 2013-02-25 20:25:46
This:http://pastebin.com/GVK60wdd – EnchanterIO 2013-02-25 20:33:01
所以如果你用'for'循環,但用'{%for friend in friends%}',你可以看到你正在尋找的'follower.nick'? – cheesemacfly 2013-02-25 20:49:24