/** * Update the $node * * @param ObjectManager $om * @param object $node - target node * @param AdapterInterface $ea - event adapter * * @return void */ public function updateNode(ObjectManager $om, $node, AdapterInterface $ea) { $oid = spl_object_hash($node); $meta = $om->getClassMetadata(get_class($node)); $config = $this->listener->getConfiguration($om, $meta->name); $uow = $om->getUnitOfWork(); $parentProp = $meta->getReflectionProperty($config['parent']); $parentProp->setAccessible(true); $parent = $parentProp->getValue($node); $pathProp = $meta->getReflectionProperty($config['path']); $pathProp->setAccessible(true); $pathSourceProp = $meta->getReflectionProperty($config['path_source']); $pathSourceProp->setAccessible(true); $path = $pathSourceProp->getValue($node); // We need to avoid the presence of the path separator in the path source if (strpos($path, $config['path_separator']) !== false) { $msg = 'You can\'t use the Path separator ("%s") as a character for your PathSource field value.'; throw new RuntimeException(sprintf($msg, $config['path_separator'])); } $fieldMapping = $meta->getFieldMapping($config['path_source']); // default behavior: if PathSource field is a string, we append the ID to the path // path_append_id is true: always append id // path_append_id is false: never append id if ($config['path_append_id'] === true || $fieldMapping['type'] === 'string' && $config['path_append_id'] !== false) { if (method_exists($meta, 'getIdentifierValue')) { $identifier = $meta->getIdentifierValue($node); } else { $identifierProp = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName()); $identifierProp->setAccessible(true); $identifier = $identifierProp->getValue($node); } $path .= '-' . $identifier; } if ($parent) { // Ensure parent has been initialized in the case where it's a proxy $om->initializeObject($parent); $changeSet = $uow->isScheduledForUpdate($parent) ? $ea->getObjectChangeSet($uow, $parent) : false; $pathOrPathSourceHasChanged = $changeSet && (isset($changeSet[$config['path_source']]) || isset($changeSet[$config['path']])); if ($pathOrPathSourceHasChanged || !$pathProp->getValue($parent)) { $this->updateNode($om, $parent, $ea); } $parentPath = $pathProp->getValue($parent); // if parent path not ends with separator if ($parentPath[strlen($parentPath) - 1] !== $config['path_separator']) { // add separator $path = $pathProp->getValue($parent) . $config['path_separator'] . $path; } else { // don't add separator $path = $pathProp->getValue($parent) . $path; } } if ($config['path_starts_with_separator'] && (strlen($path) > 0 && $path[0] !== $config['path_separator'])) { $path = $config['path_separator'] . $path; } if ($config['path_ends_with_separator'] && $path[strlen($path) - 1] !== $config['path_separator']) { $path .= $config['path_separator']; } $pathProp->setValue($node, $path); $changes = array($config['path'] => array(null, $path)); if (isset($config['path_hash'])) { $pathHash = md5($path); $pathHashProp = $meta->getReflectionProperty($config['path_hash']); $pathHashProp->setAccessible(true); $pathHashProp->setValue($node, $pathHash); $changes[$config['path_hash']] = array(null, $pathHash); } if (isset($config['level'])) { $level = substr_count($path, $config['path_separator']); $levelProp = $meta->getReflectionProperty($config['level']); $levelProp->setAccessible(true); $levelProp->setValue($node, $level); $changes[$config['level']] = array(null, $level); } if (!$uow instanceof MongoDBUnitOfWork) { $ea->setOriginalObjectProperty($uow, $oid, $config['path'], $path); $uow->scheduleExtraUpdate($node, $changes); } else { $ea->recomputeSingleObjectChangeSet($uow, $meta, $node); } if (isset($config['path_hash'])) { $ea->setOriginalObjectProperty($uow, $oid, $config['path_hash'], $pathHash); } }
/** * Update the $node * * @param ObjectManager $om * @param object $node - target node * @param object $ea - event adapter * @return void */ public function updateNode(ObjectManager $om, $node, AdapterInterface $ea) { $oid = spl_object_hash($node); $meta = $om->getClassMetadata(get_class($node)); $config = $this->listener->getConfiguration($om, $meta->name); $uow = $om->getUnitOfWork(); $parentProp = $meta->getReflectionProperty($config['parent']); $parentProp->setAccessible(true); $parent = $parentProp->getValue($node); $pathProp = $meta->getReflectionProperty($config['path']); $pathProp->setAccessible(true); $pathSourceProp = $meta->getReflectionProperty($config['path_source']); $pathSourceProp->setAccessible(true); $path = $pathSourceProp->getValue($node); // We need to avoid the presence of the path separator in the path source if (strpos($path, $config['path_separator']) !== false) { $msg = 'You can\'t use the Path separator ("%s") as a character for your PathSource field value.'; throw new RuntimeException(sprintf($msg, $config['path_separator'])); } $fieldMapping = $meta->getFieldMapping($config['path_source']); // If PathSource field is a string, we append the ID to the path if ($fieldMapping['type'] === 'string') { if (method_exists($meta, 'getIdentifierValue')) { $identifier = $meta->getIdentifierValue($node); } else { $identifierProp = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName()); $identifierProp->setAccessible(true); $identifier = $identifierProp->getValue($node); } $path .= '-' . $identifier; } $path .= $config['path_separator']; if ($parent) { $changeSet = $uow->isScheduledForUpdate($parent) ? $ea->getObjectChangeSet($uow, $parent) : false; $pathOrPathSourceHasChanged = $changeSet && (isset($changeSet[$config['path_source']]) || isset($changeSet[$config['path']])); if ($pathOrPathSourceHasChanged || !$pathProp->getValue($parent)) { $this->updateNode($om, $parent, $ea); } $path = $pathProp->getValue($parent) . $path; } $pathProp->setValue($node, $path); $changes = array($config['path'] => array(null, $path)); if (isset($config['level'])) { $level = substr_count($path, $config['path_separator']); $levelProp = $meta->getReflectionProperty($config['level']); $levelProp->setAccessible(true); $levelProp->setValue($node, $level); $changes[$config['level']] = array(null, $level); } $uow->scheduleExtraUpdate($node, $changes); $ea->setOriginalObjectProperty($uow, $oid, $config['path'], $path); }