0

I hope to get Id from table User using repository in my action :

$user = $this->getDoctrine()->getRepository('AppMyBundle:User')
    ->findByUsername($token['name']);

 $id = $user['id'];  // also $user->id or $user->getId() give me error?

that give me this error :

Notice: Undefined index: id in /var/www/project/src/App/MyBundle/Controller/DefaultController.php line 56 

finally the var_daump output :

echo "<pre>";
var_dump($user);
exit();
echo "</pre>";


 array(1) {
[0]=>
object(App\MyBundle\Entity\User)#351 (21) {
["id":protected]=>
int(17)
....
....
4

1 回答 1

2

我想你想调用findOneByUsernamemethod 而不是findByUsername.

findBy*返回一个对象数组并findOneBy*返回一个对象。

在此之后为了获取 id 只需调用你的 getter$id = $user->getId();

跳它很有帮助。

最良好的问候。

于 2013-05-09T16:03:43.330 回答