我正在尝试执行以下查询:
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; }
生成我的所有通知:
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 )); }
这个想法是在AdUser表上进行搜索,该表返回所有记录了用户拥有的广告的通知以及记录用户请求的所有通知。
通知用户请求的是一行AdUser表,其中包含用户登录User的列。
我决定打断两次搜索,然后在结果中大放异彩
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); }
为了提高可读性,只需将区分两种通知的内容放在页面上进行处理。
我发现有一种方法可以将命令添加到该方法中,这是不存在的,但是如果有人知道这样做的话,这似乎很复杂,请给出答案。