Getting Objects from database in Symfony2

1.6k views Asked by At

I had read somewhere some day that symfony2/Doctrine2 has a method(i don't remember the method name now) that fetches all "like" objects that we specify.. For example, I have User entity that has userName , password, name, state and city as properties.. For getting all Users who has name = "vinay" and state = "karnataka", the steps goes like this,,

$user = new User();
$user->setName("vinay");
$user->setState("karnataka");

$query = $em->dontKnowTheMethod($user);
$usersList = $query->getResult();

$usersList should contain all the users whose name = "vinay" and state = "karnataka"

I searched for hours but Im not getting that method.. Im sure I had read about that method long back but I cannot recall now..

Thanks in advance..

1

There are 1 answers

8
BentCoder On BEST ANSWER

You should start studying doctrine and symfony.

$user = new User();
$user->setName("vinay");
$user->setState("karnataka");

$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();

$repo = $this->getDoctrine()->getRepository('YourWhateverBundle:User');
$userResult = $repo->findAll(['name' => 'vinay', 'state' => 'karnataka'])

if (!$userResult instanceof User) {
   echo 'No result found';
} else {
   // Do whatever you want with $userResult
}