We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

toArray method with releations

Hey guys! I have a 2 Models with a hasMany relation, if I call $mymodel->getRelationField() I get my relation data as expected, but if I call $mymodel->toArray() it just dont return the associated data, how can I achieve this?

Kind regards cefuroX



43.9k

Hi,

try $mymodel->getRelationField()->toArray();

edited Feb '15

Hey thanks for your response. But I already know this and I think I have to be more specific:

This is how my models look like. I actually don't want to recursively walk through every model an check if I have to get relations! "->toArray()" seems to ignore that there is a relation, because if I look into the result of "$myfirstmodel->findFirst(1);" it shows me that "relationFIeld" is actually an object in that result, but if I make an "$myfirstmodel->findFirst(1)->toArray()" the "relationFIeld" is not an array but the original ID.

Is there a "smarter" solution instead of walking down all relations? (I have to get all model data at once!)

I don't think it's possible without some recursion. But it's slow since multiple queries are made.

If you want to get everything all at once at this point in time it might be best to write a PHQL or raw SQL query.