4

I can't seem to nail the exact rule of thumb as to what is added to an array when using toArray(), insomuch that if I was to do something like this:

$sheep = SheepQuery::create()->find();
foreach ($sheep as $sheepii) {
  $sheepii->getShepherd();
}
return $sheep->toArray();

or some variation of it, sometimes I seem to get results with both the columns from the sheep table and their sub-arrays with the shepherd, but sometimes it doesn't seem to include it, so I have to do something like add the values to a new array as I loop over.

Can anyone point out (a link explaining) why this happens and the rules behind it so I'm not blindly guessing, and if there's a nicer way of doing the same thing above by all means I'm open to suggestion.

LeonardChallis
  • 7,759
  • 6
  • 45
  • 76

1 Answers1

3

Usually, if you want a relation to be inside the toArray() you need to fetch it or make a join inside the initial query.

This will return only information for objects Sheep:

$sheep = SheepQuery::create()
  ->find();
$sheep->toArray();

This will return informations for objects Sheep and their relation to Shepherd if there is one:

$sheep = SheepQuery::create()
  ->find()
  ->joinWith('Shepherd');
$sheep->toArray();

And here is a good blog post about Getting To Know Propel 1.5: When You Really Need Arrays.

j0k
  • 22,600
  • 28
  • 79
  • 90