2013-07-22 20 views
12

benim isteğiSymfony2'nin Doktrin atış NonUniqueResultException

public function getLastViewUpdate($view) 
{ 
    $qb = $this->getEntityManager()->createQueryBuilder(); 

    $result = $qb->select('vu') 
     ->from('EasyApp\ApplicationBundle\Entity\ViewUpdate', 'vu') 
     ->where('vu.view = :view') 
     ->orderBy('vu.date','DESC') 
     ->setParameter('view', $view) 
     ->getQuery() 
     ->getSingleResult(); 

    return $result; 
} 

yılında NonUniqueResultException atan bir sorun var Ama bir şeyler almak için belki neden, ben gerçekten bilmiyorum, ama ben bulamıyorum

CRITICAL - Uncaught PHP Exception Doctrine\ORM\NonUniqueResultException: "" at /Users/antoine/Documents/projects/easyApp/application/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php line 621 

Yardımlarınız için teşekkürler:

cevap

32

Sen sorgulamak ve ->setMaxResults(1) yalnızca bir sonuç almak için LIMIT ayarlayabilirsiniz sorunu çözmek için getSingleResult fonksiyonu

/** 
* Gets the single result of the query. 
* 
* Enforces the presence as well as the uniqueness of the result. 
* 
* If the result is not unique, a NonUniqueResultException is thrown. 
* If there is no result, a NoResultException is thrown. 
* 
* @param integer $hydrationMode 
* @return mixed 
* @throws NonUniqueResultException If the query result is not unique. 
* @throws NoResultException If the query returned no result. 
*/ 
public function getSingleResult($hydrationMode = null) 
{ 
    ... 
    if (count($result) > 1) { 
     throw new NonUniqueResultException; 
    } 
    ... 
} 

ilanını kontrol edebilirsiniz.

+0

Teşekkür ancak bu çok uygun değil, neden bir hata attığını anlamıyorum kritik bir hata oluşturmak – Ajouve

+1

Onun bir hata değil, onun istisnası. 'getSingleResult' kodunu kontrol edin, cevabımı güncelledim. –

+0

Tamam, ama benim sayfam getirme değil ve symfony içinde bir 500 hatam var – Ajouve

1

Yalnızca iki veya daha fazla ViewUpdates'inizin aynı görünüme sahip olduğu anlamına gelir.

6

1'den fazla sonuç beklerseniz, getSingleResult'u kullanmayın ... Bu işlevi kullanarak sonucun bir unicity denetimi gerçekleştirir, bu işlevin amacıdır.

Birçok seçenek:

  • Kullanım getSingleResult ve try {...} catch (NonUniuqueResultException $e) {...} gibi (istisna ile uğraşmak veya yinelemeleri önlemek için DB yapısını ayarlamak,
  • Kullanım getSingleResult ve setMaxResults(1) eklemek, ama bu gerçekten güven garip bir yolu DB modeli
  • Kullanım getResult ve döndürülen sonuçları ile yaptığımız bir şey. çalıştığını
+1

Cevabınız için teşekkürler – Ajouve