function map(\Web\UI\DocumentElement $node) { $mappedObject = parent::map($node, true); if ($node->hasChildren()) { foreach ($node->getChildren() as $child) { if ($this->has($child->Oid)) { $mapped = $this->get($child->Oid); if ($mapped instanceof \ORM\Entity\EntityAttribute) { $mappedObject->addAttribute($mapped); } elseif ($mapped instanceof \ORM\Entity\EntityRelationship) { $mapped->setAssociation($this->get($node->Oid)); $mappedObject->addRelation($mapped); } } } } if ($mappedObject instanceof \ORM\Entity\EntityObject) { $attrClass = $this->getBindingClass('attribute'); if ($mappedObject->getPrimaryKey() == null) { $mappedObject->addAttribute(new $attrClass('id', 'integer', \ORM\Entity\EntityAttribute::PrimaryKey, \ORM\Entity\EntityAttribute::AutoIncrement, \ORM\Entity\EntityAttribute::NotNull)); } if (is_string($mappedBehaviors = $mappedObject->getBehaviors())) { $behaviors = array(); $behavior_names = explode(' ', $mappedBehaviors); if (is_array($behavior_names)) { foreach ($behavior_names as $name) { if (strtolower($name) == 'slug') { $behaviors[] = \ORM\Entity\EntityObject::Slug; } elseif (strtolower($name) == 'timestamp') { $behaviors[] = \ORM\Entity\EntityObject::TimeStamp; } elseif (strtolower($name) == 'nestedset') { $behaviors[] = \ORM\Entity\EntityObject::NestedSet; } } } $mappedObject->setBehaviors($behaviors); } if ($mappedObject->hasBehavior(\ORM\Entity\EntityObject::TimeStamp)) { $created = new $attrClass('created_at', 'timestamp'); $updated = new $attrClass('updated_at', 'timestamp'); $created->setDefault('0000-00-00 00:00:00'); $updated->setDefault('CURRENT_TIMESTAMP'); $created->addFlag(\ORM\Entity\EntityAttribute::NotNull); $updated->addFlag(\ORM\Entity\EntityAttribute::NotNull); $mappedObject->addAttribute($created); $mappedObject->addAttribute($updated); } if ($mappedObject->hasBehavior(\ORM\Entity\EntityObject::NestedSet)) { $mappedObject->addAttribute(new $attrClass('lft', 'integer')); $mappedObject->addAttribute(new $attrClass('rgt', 'integer')); } //$entityManager = \Core\Manager\EntityManager::getInstance(); //$entityManager->addEntity( $mappedObject ); $relationMapper = new SQLRelationshipMapper(); $mappedObject = $relationMapper->map($mappedObject); } return $mappedObject; }
function map(SqlEntity $entity, SqlEntity $changes) { // TODO: MOVE TO CORRESPONDING ATTRIBUTE AND RELATIONSHIP MAPPER // MAP ATTRIBUTES $foundAttributes = array(); $this->_changes = array(); foreach ($entity->Attributes as $name => $attr) { if (!is_string($name)) { $name = $attr->InnerName; } $changeAttr = null; if (array_key_exists($name, $changes->Attributes)) { $changeAttr = $changes->Attributes[$name]; $foundAttributes[$name] = $changedAttr; } //else // TODO: CHECK DIRTY NAME ON CHANGED ATTRIBUTE FOR MATCH WITH OLD ATTRIBUTE $changedAttribute = $this->mapAttribute($attr, $changeAttr); if ($changedAttribute !== null) { $entity->Attributes[$name] = $changedAttribute; } } $newAttributes = array_diff_key($changes->Attributes, $foundAttributes); foreach ($newAttributes as $attr) { $entity->Attributes[$attr->InnerName] = $this->mapAttribute(null, $attr); } // MAP RELATIONS $relationshipMapper = new SQLRelationshipMapper(); $entity = $relationshipMapper->map($entity); $foundRelations = array(); foreach ($entity->Relations as $name => $relation) { if (!is_string($name)) { $name = $relation->Name; } $changeRelation = null; if (array_key_exists($name, $changes->Relations)) { $changeRelation = $changes->Relations[$name]; $foundRelations[$name] = $changeRelation; } $changedRelation = $this->mapRelation($relation, $changeRelation); if ($changedRelation !== null) { $entity->Relations[$name] = $changeRelation; } } $newRelations = array_diff_key($changes->Relations, $foundRelations); foreach ($newRelations as $relation) { $entity->Relations[$relation->Name] = $this->mapRelation(null, $relation); } return $entity; }