Created
March 2, 2012 17:10
-
-
Save dariomncs/1959705 to your computer and use it in GitHub Desktop.
CakePHP Mongodb - update nested attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateSetArguments($data) { | |
$fieldsAndValues = array(); | |
$iterator = new RecursiveIteratorIterator(new RecursiveArrayOnlyIterator($data)); | |
foreach ($iterator as $value) { | |
$path = array(); | |
foreach (range(0, $iterator->getDepth()) as $depth) { | |
$path[] = $iterator->getSubIterator($depth)->key(); | |
} | |
$path = implode('.', $path); | |
$fieldsAndValues[$path] = $value; | |
} | |
return $fieldsAndValues; | |
} | |
// and a iterator to preserve object values | |
class RecursiveArrayOnlyIterator extends RecursiveArrayIterator | |
{ | |
/** | |
* Override RecursiveArrayIterator | |
* @return bool | |
*/ | |
public function hasChildren() | |
{ | |
$current = $this->current(); | |
return !empty($current) && is_array($current); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment