2013-07-11 69 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