Closed joesonw closed 10 years ago
Maybe we can extend toArray() with parameter $loadRef = false .
@joesonw @purekid this has already been discussed and added in a patch way see #48
The issue with this is two part, which #48 addresses, 1. which references do you want to load and 2. recursion.
So in the time being the easiest solution is to add this logic manually which allows the developer to control the above issues:
$docCur = Model::find();
$result = array();
foreach($docCur as $doc) {
$array = $doc->toArray();
$doc['ref1'] = $doc->ref1->toArray();
$ref2['ref2'] = $doc->ref2->toArray();
//...
$result[] = $array;
}
But a better designed solution would be to create something like this where you map out what you want the data to look like.
$doc->toArray(array(
'ref1' => array(
'load' => true,
'map' => array(
'ref1a' => true,
'ref1b' => array('load' => true, 'map' => array('ref1b1' => true))
)
)
));
We need to figure out caching so if ref1a is requested already we can load from cache rather than make another database call
On a completely separate note I often use the $id and $ref above to make Async requests from my frontend for more information. Or I mash it up in the frontend based on that data.
@joesonw reopen if you still need help
I have a function need to return a model in json (with all fields, several of them are references). The output, however, only gives "$id" and "$ref" fields instead of the referenced object.