2013-07-11 45 views
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; 
} 

}

回答

0

我认为你想为你的追随者实体使用custom repository

喜欢的东西:

/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); 
+0

其实已经想到关于这个......但我有很多使用实体方法的模板。 (user.getFollowers | count)。所以我需要修改实体的方法来适应这种新的行为。 – Nico