Take the 2-minute tour ×

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

$user = $this->getDoctrine()->getRepository('AppMyBundle:User')

 $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>";
echo "</pre>";

 array(1) {
object(App\MyBundle\Entity\User)#351 (21) {
share|improve this question
did $user->getId() give you an error? If so, do you have this method inside your User class? –  DonCallisto May 9 '13 at 14:18
sure I have,I use FosUserBudle for that... –  Nll May 9 '13 at 14:34
please if u dont have a good solution dont reply... –  Nll May 9 '13 at 14:38
This isn't stricly a SYmfony2 related question. If your var_dump is correct, then you haven't working methods for this object: it's logic, my dear. BTW, you can't access to an object with [] (array notation) –  DonCallisto May 9 '13 at 14:39
I know it's an array but when you dont have a solution you try also the bad one,you do you have a good solution plz if you don't so don't reply –  Nll May 9 '13 at 14:48

1 Answer 1

up vote 1 down vote accepted

I think you want to call findOneByUsername method instead of findByUsername.

findBy* return an array of object and findOneBy* return an object.

After this in order to get the id just call your getter $id = $user->getId();

Hop it's helpful.

Best regard.

share|improve this answer

Your Answer


