Reputation: 1054
ok, with
$user = $this->getDoctrine()
->getRepository('UserBundle:User')
->find($user_id)
I get an User by the given Identifier (ID).
but how can I get him by given email?
$emailCheck = $em->createQueryBuilder('u')
->select('u, r')
->where('u.email = :email')
->setParameter('email','[email protected]')
->getQuery();
Whill it return an array or an object? Is this the right way to handle it?
Upvotes: 1
Views: 10702
Reputation: 191
Either you use the magic method of Doctrine findOneByyour_field or you can create your own method in your repo
public function findOneByEmailAndStoreId($email, $store_id)
{
$q = $this->createQueryBuilder('c')
->where('c.email = :email')
->andWhere('c.store_id = :store_id')
->setParameter('email', $email)
->setParameter('store_id', $store_id)
->getQuery();
return $q->getOneOrNullResult(); // will return only one result or null 'getResult' will return a collection
}
Upvotes: 2
Reputation: 13117
You can use either
$user = $this->getDoctrine()
->getRepository('UserBundle:User')
->findBy(array('email' => $email));
To load an array of User entities. (Will always be a list, even with 0 or 1 results.)
Or you can do:
$user = $this->getDoctrine()
->getRepository('UserBundle:User')
->findOneBy(array('email' => $email));
This will return the first found result as a User entity object.
You can use the getOneOrNullResult()
, as well, it's the long variant. Note however that you should always use setMaxResults(1)
with this, otherwise you'll get an exception if more than one result is found (SF 2.3.x).
Upvotes: 4
Reputation: 2939
This will return a User
object.
$user = $this->getDoctrine()
->getRepository('UserBundle:User')
->findOneByEmail($email);
Upvotes: 3