2015-09-03 20 views
6

Tôi đang cố gắng để thực hiện truy vấn sau đây:Làm thế nào để thực hiện một UNION với Doctrine?

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; 
    } 

để tạo ra tất cả các thông báo của tôi:

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 
     )); 
    } 

Ý tưởng là để thực hiện tìm kiếm trên bàn AdUser trả về tất cả các thông báo có quảng cáo đăng nhập Người dùng sở hữu, cùng với bất kỳ thông báo nào đã đăng nhập Người dùng đã yêu cầu.

Thông báo Người dùng được yêu cầu là một dòng của bảng AdUser có cột mà người dùng đã đăng nhập vào Người dùng.

+1

Ngôn ngữ truy vấn giáo lý không hỗ trợ từ khóa 'UNION'. Một giải pháp có thể được tìm thấy tại http://stackoverflow.com/questions/4155288/how-to-write-union-in-doctrine-2-0 –

+0

Đây sẽ là một vấn đề mà bạn biết sẽ là một số cách để làm những gì Tôi muốn? Các phản ứng từ pergunda khác không cuối cùng giúp tôi soulação nó đã phá vỡ bảng của mình trong hai và tôi không muốn nó, chỉ muốn tạo ra một danh sách với hai truy vấn. –

+0

Bạn có muốn 20 thông báo đầu tiên mà Người dùng đã đăng nhập sở hữu quảng cáo không, sau đó là 20 thông báo đầu tiên mà Người dùng đã đăng nhập đã yêu cầu? Nếu vậy, tôi không hiểu tại sao bạn muốn kết quả này sau đó giới hạn trong 20 thông báo đầu tiên của các thông báo này ('LIMIT' cuối cùng) của bạn. 'Notofication' có nghĩa là 'n'? –

Trả lời

2

tôi quyết định phá vỡ trong hai tìm kiếm và đưa ra một Marge trong kết quả

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); 
} 

Để ngày càng trở nên readable'll chỉ cần đặt sau khi một cái gì đó mà phân biệt hai loại thông báo để thực hiện xử lý trên trang.

Tôi phát hiện ra rằng có một cách để thêm lệnh vào học thuyết không tồn tại, nhưng có vẻ khá phức tạp nếu có ai biết làm điều này, hãy đặt câu trả lời.

Các vấn đề liên quan