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>
} }
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.