Symfony主义实体朋友hasFriend关注者 [英] Symfony Doctrine entity friend hasFriend followers

查看:73
本文介绍了Symfony主义实体朋友hasFriend关注者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置关注者,在我的项目中遵循(myFriends)hasFriend逻辑。 odobera一栏的意思是跟随到昵称(id用户)odobera(跟随此用户)。用户(25)跟随用户(37)。

I need to set follower, following (myFriends) hasFriend logic to my project. column "odobera" means "following" to example nick(id user) odobera (following to this user). User (25) is following user(37).

请求表:

 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

What is good: returns 1 record. I actually follow just one person as you can see here the id of record is 24395

DB请求表:

我不知道getMyFriends函数以该格式返回响应是否良好。请仔细查看它。

I don't know if is good that getMyFriends function returns response in that "format". Please look at it carefully.

然后我从查询和循环中选择了关注者:

Then I have select followers from query and in loop:

{% 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)) %} 

返回false,为什么?当我使用dump:P检入控制器时,我跟随该用户。

That returns false, why? I follow this user as I checked in controller with dump :P Few lines over.

推荐答案

问题似乎是您在比较两个不同类型的变量。

The problem seems to be that you are comparing two different type variable.

执行此操作时: {%if(app.user.hasFriend(follower.nick))%} 会调用以下函数:

When you do this: {% if (app.user.hasFriend(follower.nick)) %} the following function is called:

/**
 * 
 * @param \TB\UserBundle\Entity\User $user
 * @return bool
 */
public function hasFriend(User $user)
{
    return $this->myFriends->contains($user);
}

此函数被调用,以个用户键入 $ user 变量,然后在 contains()函数> $ this-> myFriends 。

$ this-> myFriends ArrayCollection 请求(与 User 的类型不同),以及关于 contains()

This function is called, taking a User type $user variable and you then use the contains() function on $this->myFriends.
$this->myFriends is an ArrayCollection of Requests (so different type than User) and from the doctrine documentation about contains():


两个元素的比较严格,这意味着不仅
的值,但类型也必须匹配。

The comparison of two elements is strict, that means not only the value but also the type must match.

http://www.doctrine-project.org/api/common/2.1/class-Doctrine.Common。 Collections.ArrayCollection.html

这篇关于Symfony主义实体朋友hasFriend关注者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆