简体   繁体   中英

PHP Doctrine toArray problem

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

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);

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));
$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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM