Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2, Doctrine 2: getResult Object

$posts = $em->find('Application\BlogBundle\Entity\Post',1);
print_r ($posts);

Why I got it?

Barii\BlogBundle\Entity\Post Object ( [id:Barii\BlogBundle\Entity\Post:private] => 1 [title:Application\BlogBundle\Entity\Post:private] => something [body:Application\BlogBundle\Entity\Post:private] => content  )

instead of a simple array like this:

array ( [id] => 1,
        [title] => "something",            
        [body] => "content"  )

I use it with Symfony 2.

like image 469
barii Avatar asked May 14 '11 19:05

barii


1 Answers

You have a couple options here. As far as I know, you can't find results as arrays from entity repositories by default. Instead, you can do one of two things:

First, you could implement a toArray() method on your entity object (perhaps through a mapped superclass) that simply returns an array of properties.

Second, you could use Doctrine Query Language to pull the information that you need using the getArrayResult() method, perhaps something like this:

$query = $em->createQuery('SELECT p FROM Application\BlogBundle\Entity\Post p WHERE p.id=:pid');
$query->setParameter('tid', $postId);
$result = $query->getArrayResult(); // shortcut for $query->getResult(Query::HYDRATE_ARRAY);

More in-depth documentation on DQL can be found here.

like image 61
Derek Stobbe Avatar answered Sep 18 '22 22:09

Derek Stobbe