2012-01-15 59 views

回答

1

只需使用 - > setMaxResults(1)說 「極限」 教義

與DOC「:http://www.doctrine-project.org/docs/orm/2.1/en/reference/query-builder.html#limiting-the-result ^^

但在這種情況下,你應該使用 - 的> getSingleResult()instide - > getResult(),不使用限制。 (它更適用於!)

+0

但是,如果沒有結果,getSingleResult()將拋出EntityNotFoundException。天氣它可以返回null? – Gina 2012-01-16 02:26:42

+0

其實你總是要用doctrine2來做一次嘗試測試^^像這樣: 試一試{qq-> getResult(); (\ Doctrine \ ORM \ NoResultException $ e){ return null; } – Sybio 2012-01-16 08:29:18

+0

好的,非常感謝! – Gina 2012-01-17 00:43:48