按另一个实体的关系查找实体

时间:2016-02-28 02:40:59

标签: php symfony doctrine-orm doctrine

我有以下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设置为$interval的所有20

我该怎么办?

1 个答案:

答案 0 :(得分:1)

使用doctrine查询构建器。

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