/** @inheritdoc */ public function doPersist(IEntity $entity) { if (!$entity->isModified()) { return; } $isPersisted = $entity->isPersisted(); $this->doFireEvent($entity, $isPersisted ? 'onBeforeUpdate' : 'onBeforeInsert'); $isPersisted && $this->identityMap->remove($entity->getPersistedId()); // id can change in composite key $id = $this->mapper->persist($entity); $entity->fireEvent('onPersist', [$id]); $this->identityMap->add($entity); $this->entitiesToFlush[0][] = $entity; $this->doFireEvent($entity, $isPersisted ? 'onAfterUpdate' : 'onAfterInsert'); $this->doFireEvent($entity, 'onAfterPersist'); }
protected function entityToArray(IEntity $entity) { $return = []; $metadata = $entity->getMetadata(); foreach ($metadata->getProperties() as $name => $metadataProperty) { if ($metadataProperty->isVirtual) { continue; } elseif ($metadataProperty->isPrimary && ($entity->isPersisted() || !$entity->hasValue($name))) { continue; } elseif ($entity->isPersisted() && !$entity->isModified($name)) { continue; } if ($metadataProperty->relationship !== null) { $rel = $metadataProperty->relationship; $canSkip = $rel->type === Relationship::ONE_HAS_MANY || $rel->type === Relationship::MANY_HAS_MANY || $rel->type === Relationship::ONE_HAS_ONE && !$rel->isMain; if ($canSkip) { continue; } } $property = $entity->getProperty($name); if ($property instanceof IProperty) { $value = $property->getRawValue(); } else { $value = $entity->getValue($name); } $return[$name] = $value; } return $return; }
public function persist(IEntity $entity) { if (!$entity->isModified()) { $entity->id; } $this->beginTransaction(); $id = $entity->getValue('id', TRUE); $data = $entity->toArray(IEntity::TO_ARRAY_LOADED_RELATIONSHIP_AS_IS); $storageProperties = $entity->getMetadata()->getStorageProperties(); foreach ($data as $key => $value) { if (!in_array($key, $storageProperties, TRUE) || $value instanceof IRelationshipCollection) { unset($data[$key]); } if ($value instanceof IEntity) { $data[$key] = $value->id; } } unset($data['id']); $data = $this->getStorageReflection()->convertEntityToStorage($data); if (!$entity->isPersisted()) { $this->databaseContext->query('INSERT INTO ' . $this->getTableName() . ' ', $data); if ($id) { return $id; } else { return $this->databaseContext->getInsertId($this->databaseStructure->getPrimaryKeySequence($this->getTableName())); } } else { $primary = []; $id = (array) $id; foreach ($this->getStorageReflection()->getStoragePrimaryKey() as $key) { $primary[$key] = array_shift($id); } $this->databaseContext->query('UPDATE ' . $this->getTableName() . ' SET', $data, 'WHERE ?', $primary); return $entity->id; } }