dongyao2129 2016-05-20 19:43
浏览 53
已采纳

错误:在非对象[重复]上调用成员函数getId()

I am trying to get the id from a entity object.

The Code

public function findByName($name)
{
    $qb = $this->createQueryBuilder('u');
    $qb->where('u.name  = :name');
    $qp->setParameter('name', $name);
    return $qb->getQuery()->getResult();
}

And I am calling the function like this

$Obj = $this->getDoctrine()->getRepository('UserBundle:User');
$idName=$Obj->findByName('Sarah');
var_dump($idName->getId());

I am getting an error for this part var_dump($idName->getId());.

I am getting the whole Object and I just want the Id. It doesn't work.

$idName dumps to this:

@array(1) {<br>
[0]=> object(UserBundle\Entity\User)#2339 (2) { <br>
["id":"UserBundle\Entity\User":private]=> int(2) ["name":"UserBundle\Entity\User":private]=> string(8) "Sarah" <br>
} }
</div>
  • 写回答

2条回答 默认 最新

  • douxi0098 2016-05-20 22:19
    关注

    findBy... always returns an array of found entities (even if there is just one found for the criteria). Use findOneBy... to always get only the first entity returned.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据