/** * {@inheritDoc} * * Persist and flush (persisting an already managed document has no effect * and does not hurt). * * @throws \Exception will throw some exception if storing fails, type is * depending on the doctrine implemenation. */ public function store(EntityInterface $entity) { $this->om->persist($entity->getObject()); $this->om->flush(); return true; }
private function _storeData($new_values, EntityInterface $entity) { $object = $entity->getObject(); foreach ($entity->getChildDefinitions() as $fieldname => $node) { if ($node instanceof CollectionInterface) { // check for the list of children. The order may have changed $rel = $node->getRel(); $expanded_name = $this->_expandPropertyName($rel, $entity); if (array_key_exists($expanded_name, $new_values)) { $expectedOrder = $new_values[$expanded_name]; array_walk($expectedOrder, array($this, 'walkJsonLdDecode')); $this->_mapper->orderChildren($entity, $node, $expectedOrder); } } elseif ($node instanceof PropertyInterface) { /** @var $node PropertyInterface */ $rdf_name = $node->getProperty(); $expanded_name = $this->_expandPropertyName($rdf_name, $entity); if (array_key_exists($expanded_name, $new_values)) { $object = $this->_mapper->setPropertyValue($object, $node, $new_values[$expanded_name]); } } } if ($this->_mapper->store($entity)) { return $this->_convertToJsonld($object, $entity); } return null; }
/** * Reorder the children of the collection node according to the expected order. * * To make this as fail safe as possible we sort with the sort method below that makes sure * no children are deleted if they are not part in the array of passed children * ($expectedOrder). * * Also the sorting ensures that other children before the children to sort in the list * stay in front and those originally after in the back ... * * @param EntityInterface $entity * @param CollectionInterface $node * @param $expectedOrder array of subjects */ public function orderChildren(EntityInterface $entity, CollectionInterface $node, $expectedOrder) { array_walk($expectedOrder, array($this, 'getNodeName')); $childrenCollection = $this->getChildren($entity->getObject(), $node); $children = $childrenCollection->toArray(); $childrenNames = array_keys($children); $childrenNames = $this->sort($childrenNames, $expectedOrder); $childrenCollection->clear(); foreach ($childrenNames as $name) { $childrenCollection->set($name, $children[$name]); } }
/** * Never call this method directly, but use createWithParent on your CollectionDefinition * to get a collection tied to concrete data. * * @private * * @param EntityInterface $parent */ public function loadFromParent(EntityInterface $parent) { $this->_children = array(); $object = $parent->getObject(); $parentMapper = $parent->getMapper(); $children = $parentMapper->getChildren($object, $this); // create entities for children foreach ($children as $child) { if (count($this->_typenames) === 1) { $type = $this->_typeFactory->getTypeByRdf(current($this->_typenames)); } else { $type = $this->_typeFactory->getTypeByObject($child); } foreach ($type->getVocabularies() as $prefix => $uri) { $this->setAttribute('xmlns:' . $prefix, $uri); } $this->_children[] = $type->createWithObject($child); } if ($this->_parent->isEditable($object) && sizeof($this->_children) == 0 && count($this->_typenames) == 1) { // create an empty element to allow adding new elements to an empty editable collection $type = $this->_typeFactory->getTypeByRdf(reset($this->_typenames)); $mapper = $type->getMapper(); $object = $mapper->prepareObject($type, $object); $entity = $type->createWithObject($object); if ($entity instanceof NodeInterface) { /** @var $entity NodeInterface */ $entity->setAttribute('style', 'display:none'); } $this->_children[] = $entity; } }