Skip to content

Instantly share code, notes, and snippets.

Created June 27, 2017 06:27
Show Gist options
  • Save anonymous/1639da178f9e578ab16081154c7570a8 to your computer and use it in GitHub Desktop.
Save anonymous/1639da178f9e578ab16081154c7570a8 to your computer and use it in GitHub Desktop.
serialize json view cakephp3
protected function _dataToSerialize($serialize = true)
{
if ($serialize === true) {
$data = array_diff_key(
$this->viewVars,
array_flip($this->_specialVars)
);
if (empty($data)) {
return null;
}
return $data;
}
if (is_array($serialize)) {
//added
$j = 1;
$data = [];
foreach ($serialize as $alias => $key) {
if (is_numeric($alias)) {
$alias = $key;
}
if (array_key_exists($key, $this->viewVars)) {
$data[$alias] = $this->viewVars[$key];
//added
$data[$alias][$key]['recid'] = $j++;
}
}
return !empty($data) ? $data : null;
}
return isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment