Doing some eager loading for an AJAX action, the eager loading seems to work fine but then I encode to JSON it leaves out the relational objects.
Is this a bug?
Doing some eager loading for an AJAX action, the eager loading seems to work fine but then I encode to JSON it leaves out the relational objects.
Is this a bug?
Maybe this is more of a feature request…
Yes… i think there is not a good way to do this instead making a new array and encode that
Thanks!!
I felt confused when I create a new array and encode that. I didn’t know if I missed sth.
If you want to get the related objects to display in your JSON output, you can do this:
Open CJSON.php in the framework library.
modify the section of code that looks like this:
case 'object':
if ($var instanceof Traversable)
{
$vars = array();
foreach ($var as $k=>$v)
$vars[$k] = $v;
}
else
$vars = get_object_vars($var);
return '{' .
join(',', array_map(array('CJSON', 'nameValue'),
array_keys($vars),
array_values($vars)))
. '}';
case 'object':
if ($var instanceof Traversable)
{
$vars = array();
foreach ($var as $k=>$v)
$vars[$k] = $v;
}
else
$vars = get_object_vars($var);
// related
foreach ($var->relations() as $key => $related)
{
if ($var->hasRelated($key))
{
$vars[$key] = $var->$key;
}
}
return '{' .
join(',', array_map(array('CJSON', 'nameValue'),
array_keys($vars),
array_values($vars)))
. '}';
Let me know if this is what you are looking for.