douchunxian9740
2014-03-25 15:55
浏览 42
已采纳

Symfony2 + Doctrine + Repository:这个函数不应该返回对象而不是数组

I have this function in my repository:

public function findLatestFeeAsigned($company_id) {
    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb->select('c', 'chw');
    $qb->from('Configuration\FeeBundle\Entity\Fee', 'c');
    $qb->leftJoin('Company\ApprovalBundle\Entity\CompanyHasWtax', 'chw', \Doctrine\ORM\Query\Expr\Join::WITH, 'c.id = chw.wtax');
    $qb->where('chw.company = ?1');
    $qb->orderBy('chw.created', 'ASC');
    $qb->setMaxResults(1);
    $qb->setParameter(1, $company_id);

    return $qb->getQuery()->getResult();
}

And I call it from my controller in this way:

$entityCurrentFee = $em->getRepository('ApprovalBundle:CompanyHasWtax')->findLatestFeeAsigned($id);

Where $id is a request parameter. Then I try to get the some column values as:

$entityCurrentFee->getId();

But I got error saying that $entityCurrentFee is not a object, why is that?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyanju0945 2014-03-25 15:58
    已采纳

    If you're querying by the primary key or just expecting to retrieve single object, you should use

    $qb->getQuery()->getSingleResult();
    

    Beware that this function throws NoResultException

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题