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;
}
}