Write one or more new/changed Entity objects to the Datastore

@todo Consider returning the input

@param Entity|Entity[]
187438
public function upsert($entities)
    {
        if($entities instanceof Entity) {
            $entities = [$entities];
        }
        $this->obj_gateway
            ->withSchema($this->obj_schema)
            ->withTransaction($this->consumeTransaction())
            ->putMulti($entities);
    }
827464
private function writeXmlFiles()
    {
        foreach ($this->entityDefinitions as $key => $definition) {
            $definition->writeXml($this->dataDirectory . "/entities/{$definition->name}.xml");
        }
    }
930283
public function save(Entity $entity, $attributes = [])
    {
        foreach ($attributes as $key => $value) {
            $entity->$key = $value;
        }

        // when new record.
        if (! $entity->isExists()) {
            // TODO: custom primary value handler.
            if ($entity->hasAttribute('created_at')) $entity->created_at = time();
            if ($entity->hasAttribute('updated_at')) $entity->updated_at = time();
            $new = $this->create($entity->toArray());
            $entity->exists = true;
            $entity->setPrimaryValue($new->getPrimaryValue());
            
            $entity->shiftOriginalAttributes();
        }

        // when update.
        else {
            $attributes = $entity->getAttributes(true);
            if (! $attributes) return;

            if ($entity->hasAttribute('updated_at')) {
                $attributes['updated_at'] = time();
                $entity->updated_at = $attributes['updated_at'];
            }

            $entity->shiftOriginalAttributes();
            return $this->update($attributes, $entity->getPrimaryValue());
        }
    }
552034
function writeEntities(subFolder) {
  for (let i = 0, entityNames = Object.keys(configuration.entities); i < entityNames.length; i++) {
    const filePath = path.join(subFolder, toFilePath(entityNames[i]));
    const entity = updateChangelogDate(filePath, configuration.entities[entityNames[i]]);
    fs.writeFileSync(filePath, JSON.stringify(entity, null, 4));
  }
}
605844
public function saveToAndGetClonedEntities()
    {
        $data = $this->transformFromFormData($this->data);

        $cloned_entities = array();
        foreach ($this->entities as $entity) {
            $entity['entity'] = clone $entity['entity'];
            $cloned_entities[] = $entity;

            if ($entity['entity'] instanceof EntityInterface) {
                $entity['entity']->setFormData($data, $entity['fields']);
            } else {
                $this->entityProcessor->saveToEntity($entity['entity'], $data, $entity['fields']);
            }
        }

        return $cloned_entities;
    }
1371171
private function updateActiveRow(Entity $entity) {
		$reflection = $entity->getColumns();
		$record = array();
		foreach ($reflection as $property) {
			$name = $property['name'];
			if ($entity->toRow()->$name != $entity->$name) {
				$record[$name] = $entity->$name;
			}
		}
		if (count($record) > 0) {
			$entity->toRow()->update($record);
		}
		$this->addLoop($entity);
	}
395674
public function castForStore(Entity $entity)
    {
        foreach ($entity->dump() as $field => $value) {
            if (null === $value) {
                continue;
            }

            $cast = $entity->getCast($field);

            switch ($cast) {
                case 'int':
                case 'integer':
                    $entity->$field = (int) $value;
                    break;
                case 'real':
                case 'float':
                case 'double':
                case 'string':
                    $entity->$field = (string) $value;
                    break;
                case 'bool':
                case 'boolean':
                    $entity->$field = (int) $value;
                    break;
                case 'object':
                    $entity->$field = is_object($value) ? json_encode($value) : $value;
                    break;
                case 'array':
                    $entity->$field = is_array($value) ? json_encode($value) : $value;
                    break;
                case 'json':
                    if (is_string($value)) {
                        if (strlen($value) > 0 && !in_array($value[0], ['"', '{', '['], true)) {
                            $entity->$field = json_encode($value);
                            break;
                        }

                        $entity->$field = $value;
                        break;
                    }

                    $entity->$field = json_encode($value);
                    break;
                case 'date':
                case 'datetime':
                    $entity->$field = $entity->toDateTime($value)->format($this->db->getQuery(true)->getDateFormat());
                    break;
                case 'timestamp':
                    $entity->$field = $entity->toDateTime($value)->getTimestamp();
                    break;
            }
        }

        return $entity;
    }
658482
protected function saveEntity($entity)
    {
        try {
            $this->entityManager->persist($entity);
            $this->entityManager->flush($entity);
            return $entity;
        } catch (OptimisticLockException $e) {
            throw new DatabaseException($e->getMessage());
        }
    }
479209
public function persist(Entity $entity)
    {
        $this->checkEntityType($entity);

        $this->events->invokeCallbacks(Events::EVENT_BEFORE_PERSIST, $entity);
        if ($entity->isDetached()) {
            $entity->makeAlive($this->entityFactory, $this->connection, $this->mapper);
            $this->events->invokeCallbacks(Events::EVENT_BEFORE_CREATE, $entity);
            $result = $id = $this->insertIntoDatabase($entity);
            $entity->attach($id);
            $this->events->invokeCallbacks(Events::EVENT_AFTER_CREATE, $entity);
        } else {
            if ($entity->isModified()) {
                $this->events->invokeCallbacks(Events::EVENT_BEFORE_UPDATE, $entity);
                $result = $this->updateInDatabase($entity);
                $this->events->invokeCallbacks(Events::EVENT_AFTER_UPDATE, $entity);
            }
            $this->persistHasManyChanges($entity);
            $entity->markAsUpdated();
        }
        $this->events->invokeCallbacks(Events::EVENT_AFTER_PERSIST, $entity);

        return isset($result) ? $result : null;
    }
289344
public function save(Entity $entity) {
    $this->_em->persist($entity);
    $this->_em->flush();
    return $this;
  }
864666
public function write($instance, $entity = null)
    {
        list($instance, $entity) = $this->reassignEntity($instance, $entity);

        return new WriteQuery(
            $this->connection,
            $instance,
            $this->models->get($entity),
            $this->factory,
            $this->accessor,
            $this->dispatcher
        );
    }
215283
protected function addRecord($entity, array $action = [])
    {
        if ($entity instanceof $this->className) {
            if (false !== ($index = array_search($entity, array_column($this->records, 0), true))) {
                if (!in_array($this->records[$index], $action)) {
                    $this->records[$index][] = $action;
                }
            } else {
                $this->records[] = [$entity, $action];
            }
        }
    }
937877
public function insert(Entity $entity = null, Repository $repository = null)
    {
        $entity = $this->getEntityIfEmpty($entity);
        $repository = $entity->getRepositoryIfEmpty($repository);

        $this->trigger(['saving', 'inserting']);

        $insert = $repository->insert($this, $entity);

        $this->trigger(['inserted', 'saved']);

        $this->setOriginalFromData();

        return $insert;
    }
248453
public function setEntity(Entity $entity)
    {
        $this->each(
            function(Record $record) use ($entity) {
                $record->setEntity($entity);
                $record->setEntityClass(get_class($entity));
            }
        );

        return $this;
    }
1200043
protected function insert($entity)
    {
        // If this entity was a child of aggregate roots, then call its methods to set the aggregate root Id
        $this->entityRegistry->runAggregateRootCallbacks($entity);
        $className = $this->entityRegistry->getClassName($entity);
        $dataMapper = $this->getDataMapper($className);
        $idGenerator = $this->idGeneratorRegistry->getIdGenerator($className);

        if ($idGenerator === null) {
            $dataMapper->add($entity);
        } else {
            if ($idGenerator instanceof SequenceIdGenerator) {
                $idGenerator->setConnection($this->connection);
            }

            if ($idGenerator->isPostInsert()) {
                $dataMapper->add($entity);
                $this->idAccessorRegistry->setEntityId(
                    $entity,
                    $idGenerator->generate($entity)
                );
            } else {
                $this->idAccessorRegistry->setEntityId(
                    $entity,
                    $idGenerator->generate($entity)
                );
                $dataMapper->add($entity);
            }
        }

        $this->entityRegistry->registerEntity($entity);
    }
302941
protected function indexActionInternal($entities): void {
        $objects = [];
        foreach($entities as $entity) {
            $objects[] = $this->generateEntityAction($entity);
        }
        $outputObject = $this->getOutputObject();
        $blankEntity = $this->generateEntity();
        $outputObject->{$blankEntity->getEntityName() . 's'} = $objects;

        $this->setOutputObject($outputObject);
    }
1144556
public void addEntities(String[][] entityArray) {
        for (String[] anEntityArray : entityArray) {
            addEntity(anEntityArray[0], Integer.parseInt(anEntityArray[1]));
        }
    }