我有這樣的方法:期望一個或沒有從學說查詢生成器的結果,我應該使用什麼?
public function getMonth ($month_name)
{
$q = $this->createQueryBuilder('m');
$q->select('m')
->where('m.name = :name')
->setParameter('name', $month_name);
return $q->getQuery()->getResult();
}
從它,我希望找到一個月或0個月。我這樣在我的控制器使用此方法:
$month = $em->getRepository('EMExpensesBundle:Month')
->getMonth($this->findMonth());
$month->setSpended($item->getPrice());
我試圖與getSingleResult()
和一切都很完美,直到我遇到的情況來到時,沒有發現一個月,一切都失敗實在太差!
然後我試圖與getResult()
,但它返回一個數組,然後
$month->setSpended($item->getPrice());
稱非對象上調用,並修復它,我應該使用無處不在
$month[0]->setSpended($item->getPrice());
是有沒有更好的方法來實現這一點,而無需在所有地方添加無用的[0]索引?
非常感謝您! :) – Faery