tags:

views:

753

answers:

3

I have a problem with the toArray() method in Doctrine. Its doesn't get my relations:

First query :

$q = Doctrine::getTable('posts')->find(1);
debug($q->toArray(true));

Print the postid=1 with out the relations

$q = Doctrine::getTable('posts')->find(1);
$q->Tags->toArray();
debug($q->toArray(true));

...prints the results with tag relation.

But i want to do:

Doctrine::getTable('posts')->findAll()->toArray(true);

...and get all of relations of posts , instead I got an array of post row.

Any idea about how to make it work with the relations?

(notice i added toArray(true) for deep property.

thanks for any help

+1  A: 

I beleive you need to do a Join with the query. Otherwise it doesnt hydrate the realated data.

$q = Doctrine_Query::create()
    ->from('Post p')
    ->leftJoin('p.RelatedModel1 rm1')
    ->leftJoin('p.RelatedModel2 rm2');

$q->findAll()->toArray(true);
prodigitalson
+1  A: 

You could create named query for this table with all relations attached:

Doctrine::getTable('posts')->addNamedQuery('get.by.id.with.relations', 'DQL here...');

And then just use something like this:

Doctrine::getTable('posts')->find('get.by.id.with.relations', array(123));
Crozin
A: 
$q = Doctrine_Query::create()
->from('Post p')
->leftJoin('p.RelatedModel1 rm1')
->leftJoin('p.RelatedModel2 rm2');

$q->findAll()->toArray(true);

Can i Add ->limit()->offset() to the query ?

I guss that if i first create the query then findAll will act the same as execute right ?

you should be able to add anything thats valid for a query. and yes they are just differnt methods of acheiving essentially the same thing. You could even use the table class if you want i think by doing something like `Doctrine::getTable('posts')->getQuery('p')->leftJoin('p.RelatedModel1 rm1')->leftJoin('p.RelatedModel2 rm2')->findAll()->toArray(true);`
prodigitalson