2015-09-03 22 views
6

che sto cercando di fare la seguente query:Come creare UNION con 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; 
    } 

per generare tutte le notifiche:

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

L'idea è di fare una ricerca su tavolo ADUser che restituisce tutte le notifiche che hanno gli annunci che l'utente registrato è proprietario di tutte le notifiche registrate dall'utente richiesto.

Notifica L'utente richiesto è una riga di tabella AdUser che contiene la colonna dell'utente che ha effettuato l'accesso.

+1

Doctrine Query Language non supporta la parola chiave 'UNION'. Una soluzione alternativa può essere trovata su http://stackoverflow.com/questions/4155288/how-to-write-union-in-doctrine-2-0 –

+0

Questo sarà un problema, sai che sarà un modo per fare cosa Voglio? La risposta dell'altro pergunda non mi aiuta in fin dei conti a spezzare il suo tavolo in due e non lo voglio, voglio solo creare una lista con le due domande. –

+0

Desiderate le prime 20 notifiche in cui l'utente registrato possiede l'annuncio, seguito dalle prime 20 notifiche che l'utente registrato ha richiesto? Se è così, non capisco perché vorresti che questo risultato fosse limitato alle prime 20 notifiche di queste notifiche (il tuo ultimo 'LIMIT'). 'Notofication' significa essere 'n'? –

risposta

2

ho deciso di rompere in due ricerche e dando un margine nei risultati

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

Per diventare più readable'll appena messo alla ricerca di qualcosa che distingue i due tipi di preavviso per fare il trattamento sulla pagina.

Ho scoperto che c'è un modo per aggiungere comandi alla dottrina che non esiste, ma sembra essere abbastanza complesso se qualcuno sa di farlo, metti la risposta per favore.