如何与Doctrine建立联盟?

Mar*_*dro 6 sql symfony doctrine-orm

我正在尝试执行以下查询:

    public function findByNotifications($ownerId)
    {
        $em = $this->getEntityManager();
        $query = $em->createQuery('
           SELECT n FROM
            (SELECT n FROM DelivveWebBundle:UserAd n
                INNER JOIN n.ad ad
                    WHERE ad.owner = :ownerId
                LIMIT 20
            UNION
            SELECT n FROM DelivveWebBundle:UserAd n
                INNER JOIN n.user u
                INNER JOIN n.ad ad
                   WHERE u.id = :ownerId
                       AND ad.status = :progress
                LIMIT 20)
           notofication
           LIMIT 20;
        ')->setParameter('ownerId', $ownerId)
            ->setParameter('progress', Constant::AD_IN_PROGRESS);

        $result = $query->getResult();

        return $result;
    }
Run Code Online (Sandbox Code Playgroud)

生成我的所有通知:

    public function showNotificationsAction()
    {
        $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');

        $owner = $this->getUser();

        $repository = $this->getDoctrine()->getRepository('DelivveWebBundle:UserAd');

        $notifications = $repository->findByAdOwner($owner->getId());

        return $this->render('DelivveWebBundle:Ad:notification.html.twig', array(
            'owner' => $owner,
            'notifications' => $notifications
        ));
    }
Run Code Online (Sandbox Code Playgroud)

我们的想法是在AdUser表上进行搜索,该表返回包含用户拥有的广告的所有通知,以及用户请求的任何通知.

用户请求的通知是一行AdUser表,其中包含用户登录用户的列.

Mar*_*dro 2

我决定分成两次搜索并增加结果

public function findByAdOwner($ownerId)
{
    $qb = $this->getEntityManager()->createQueryBuilder('n');

    return $qb->select('n')
        ->from('DelivveWebBundle:UserAd', 'n')
        ->join('n.ad', 'ad')
        ->where('ad.owner = :ownerId')
        ->setParameter('ownerId', $ownerId)
        ->setMaxResults(20)
        ->getQuery()
        ->getResult();
}

public function findByUserNotify($userId)
{
    $qb = $this->getEntityManager()->createQueryBuilder('n');

    return $qb->select('n')
        ->from('DelivveWebBundle:UserAd', 'n')
        ->join('n.ad', 'ad')
        ->where('n.user = :userId')
        ->andWhere('ad.status = :status')
        ->setParameter('userId', $userId)
        ->setParameter('status', Constant::AD_IN_PROGRESS)
        ->setMaxResults(20)
        ->getQuery()
        ->getResult();
}

public function findNotifcations($userId){
    $notification = $this->findByAdOwner($userId);
    $append = $this->findByUserNotify($userId);

    return array_merge($notification, $append);
}
Run Code Online (Sandbox Code Playgroud)

为了变得更具可读性,只需在区分两种类型的通知之后放置一些内容即可在页面上进行处理。

我发现有一种方法可以向不存在的学说添加命令,但似乎相当复杂,如果有人知道这样做,请给出答案。