0

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

3 Answers 3

1

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));
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0
$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 ?

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.