2016-02-28 68 views
-1

我有以下Task類/實體:由另一實體的關係找到實體

/** 
* @ORM\Entity 
* @InheritanceType("SINGLE_TABLE") 
* @DiscriminatorColumn(name="discr", type="string") 
* @DiscriminatorMap({"task" = "Task", "upload" = "UploadTask", "follow" = "FollowTask", "like" = "LikeTask", "comment" = "CommentTask", "scape" = "ScrapeTask"}) 
*/ 
class Task 
{ 

    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 


    /** 
    * @ORM\ManyToOne(targetEntity="PlayBot\UserBundle\Entity\User", inversedBy="task") 
    * @ORM\JoinColumn(name="account_id", referencedColumnName="id") 
    */ 
    protected $account; 

    /** 
    * @ORM\ManyToOne(targetEntity="PlayBot\PlayBundle\Entity\Interval", inversedBy="task") 
    * @ORM\JoinColumn(name="interval_id", referencedColumnName="id", nullable=true) 
    */ 
    protected $interval = NULL; 

} 

及以下Interval類:

/** 
* @ORM\Entity 
* @ORM\Table(name="interval") 
*/ 
class Interval 
{ 

    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\OneToMany(targetEntity="PlayBot\PlayBundle\Entity\Task", mappedBy="interval") 
    */ 
    protected $task; 

    /** 
    * @ORM\Column(type="integer") 
    */ 
    protected $interval; 

} 

我想找到所有Task有一個Interval「 s $interval設置爲20

我該怎麼做?

回答

1

使用原則查詢生成器。

$tasks = $this->em->getRepository('Task')->createQueryBuilder('t') 
    ->join('t.interval', 'i') 
    ->where('i.interval = :interval') 
    ->setParameter('interval', 20) 
    ->getQuery() 
    ->getResult();