2013-08-05 33 views
5

Tôi muốn lấy một thực thể mà hiệp hội đảo ngược; không tồn tại (trên 1: hiệp hội 1)thuyết DQL: làm thế nào để có được biểu hiện sang một bên đảo ngược;

tôi nhận được lỗi:

A single-valued association path expression to an inverse side is not supported in DQL queries. Use an explicit join instead.

Truy vấn:

$query = $this->getEntityManager()->createQuery(" 
       SELECT DISTINCT(p.date) 
       FROM MainBundle:Price p 
       WHERE p.emaPrice IS NULL 
       ORDER BY p.date ASC 
      ") 
      ->setMaxResults(1); 
     $date = $query->getOneOrNullResult(); 

Tôi hiểu lỗi, nhưng tôi thực sự bị mắc kẹt khi sửa chữa. Tôi đã đọc Tôi nên thêm một JOIN rõ ràng, nhưng tôi đã thêm nó và vẫn không có may mắn.

EDIT truy vấn với tham gia:

$query = $this->getEntityManager()->createQuery(" 
     SELECT DISTINCT(p.date) 
     FROM MainBundle:Price p 
     JOIN MomentumBundle:EmaPrice ep 
     WITH ep.id = p.emaPrice 
     WHERE p.emaPrice IS NULL 
     ORDER BY p.date ASC 
    ") 
    ->setMaxResults(1); 
$date = $query->getOneOrNullResult(); 
+0

Làm thế nào để nhìn truy vấn của bạn như với việc tham gia? – Flip

+0

@Flip đã thêm truy vấn tham gia – Tjorriemorrie

+0

Bạn có thể làm cho nó hoạt động trong SQL nguyên gốc không? – Flip

Trả lời

4

Tôi muốn gửi lời cảm ơn @Flip và @lifo hỗ trợ tuyệt vời của họ trong việc giúp tôi giải quyết này:

$query = $this->getEntityManager()->createQuery(" 
     SELECT DISTINCT(p.date) 
     FROM MainBundle:Price p 
     LEFT JOIN MomentumBundle:EmaPrice ep 
     WITH ep.price = p.id 
     WHERE ep IS NULL 
     ORDER BY p.date ASC 
    ") 
    ->setMaxResults(1); 
$date = $query->getOneOrNullResult();