0

我想检索与用户关联的所有关注者,但有一定的规则。只有类型为 0 的。这是从下表中检索关注者的用户实体方法:

/**
     * @ORM\OneToMany(targetEntity="Follow", mappedBy="following")
     */
    protected $followers;


---------------


/**
     * Add Followers
     *
     * @param \Acme\UserBundle\Entity\Follow $follow
     * @return DealFlowCore
     */
    public function addFollower(\Acme\UserBundle\Entity\Follow $followers)
    {
    $this->followers[] = $followers;

    return $this;
}

/**
 * Remove Followers
 *
 * @param \Acme\UserBundle\Entity\Follow $followers
 */
public function removeFollower(\Acme\UserBundle\Entity\Follow $followers)
{
    $this->followers->removeElement($followers);
}

/**
 * Get Followers
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getFollowers()
{
    return $this->followers;
}

这是跟随实体

<?php

namespace Acme\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * FFollow
 *
 * @ORM\Table(name="f_follow")
 * @ORM\Entity
 */
class Follow
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id_follow", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer // 0 user 1 project
     *
     * @ORM\Column(name="type_follow", type="integer", nullable=false)
     */
    private $typeFollow;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime", nullable=false)
     */
    private $date;

    /**
 * @var \FUser
 *
 * @ORM\ManyToOne(targetEntity="User", inversedBy="followers")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_following", referencedColumnName="id")
 * })
 */
private $following;

/**
 * @var \FUser
 *
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_follower", referencedColumnName="id")
 * })
 */
private $follower;

public function __construct($typeFollow = 0)
{
    $this->typeFollow = $typeFollow;
}


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set typeFollow
 *
 * @param integer $typeFollow
 * @return Follow
 */
public function setTypeFollow($typeFollow)
{
    $this->typeFollow = $typeFollow;

    return $this;
}

/**
 * Get typeFollow
 *
 * @return integer 
 */
public function getTypeFollow()
{
    return $this->typeFollow;
}

/**
 * Set date
 *
 * @param \DateTime $date
 * @return Follow
 */
public function setDate($date)
{
    $this->date = $date;

    return $this;
}

/**
 * Get date
 *
 * @return \DateTime 
 */
public function getDate()
{
    return $this->date;
}

/**
 * Set following
 *
 * @param \Acme\UserBundle\Entity\User $following
 * @return Follow
 */
public function setFollowing(\Acme\UserBundle\Entity\User $following = null)
{
    $this->following = $following;

    return $this;
}

/**
 * Get following
 *
 * @return \Acme\UserBundle\Entity\User 
 */
public function getFollowing()
{
    return $this->following;
}

/**
 * Set follower
 *
 * @param \Acme\UserBundle\Entity\User $follower
 * @return Follow
 */
public function setFollower(\Acme\UserBundle\Entity\User $follower = null)
{
    $this->follower = $follower;

    return $this;
}

/**
 * Get follower
 *
 * @return \Acme\UserBundle\Entity\User 
 */
public function getFollower()
{
    return $this->follower;
}

}

4

1 回答 1

0

我认为您会希望为您的 Followers 实体使用自定义存储库。

就像是:

/Acme/UserBundle/Repository/FollowRepository.php

<?php

namespace Acme\UserBundle\Repository;


class FollowRepository extends EntityRepository
{
     public function getFollowersByUserAndType($user, $type)
     {
         $em = $this->getEntityManager();
         $query = $em->createQuery("
             SELECT f
             FROM AcmeUserBundle:Follow f
             WHERE f.following = :user 
             AND f.typeFollow = :type"
         );

         $query->setParameter('user', $user);
         $query->setParameter('type', $type);

         return $query->getResult();
     }
}

然后你可以从你的控制器调用它,比如:

$user = //get your user somehow
$type = 0
$followRepository = $this->getDoctrine()
                         ->getManager()
                         ->getRepository('AcmeUserBundle:Follow');
$followers = $followRepository->getFollowersByUserAndType($user, $type);
于 2013-07-12T07:26:06.440 回答