Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an array not an object with doctrine findOneBy method in symfony2?

I have a situation in which I want to query the database with findOneBy($id) method from doctrine in symfony2.

$namePosting = $this->getDoctrine()->getRepository('MyBundle:Users')->findOneById($userPosting);

The result it's an object with protected properties. I want to return it directly an array. How can this be done ?

like image 461
irimie andrei Avatar asked May 15 '14 14:05

irimie andrei


1 Answers

findOneBy(array()) will always return null or object.

But you can use instead findById($userPosting) or findBy(array('id' => $userPosting)) and it will return an array, e.g.:

$this->getDoctrine()->getRepository('MyBundle:Users')->findById($userPosting))

Edited

Or you can add a method in UserRepository class:

    use Doctrine\ORM\EntityRepository;
    use Doctrine\ORM\Query;

    class UserRepository extends EntityRepository
    { 
        public function getUser($userPosting)
        {
           $qb = $this->createQueryBuilder('u')
             ->select('u')
             ->where('u =:userPosting')->setParameter('userPosting', $userPosting)
             ->getQuery()
             ->getResult(Query::HYDRATE_ARRAY);

           return $qb;
        }   
    }
like image 167
Ioana Hazsda Avatar answered Sep 17 '22 08:09

Ioana Hazsda