diff --git a/src/Controller/GlobalData.php b/src/Controller/GlobalData.php index 966bfb3f093506803e28e822682991f602e88dcc..da33f76e478f80714eb87248154af73279f50308 100644 --- a/src/Controller/GlobalData.php +++ b/src/Controller/GlobalData.php @@ -7,6 +7,7 @@ namespace jtl\Connector\Example\Controller; use jtl\Connector\Core\Logger\Logger; +use jtl\Connector\Core\Model\DataModel; use jtl\Connector\Core\Model\QueryFilter; use jtl\Connector\Core\Rpc\Error; use jtl\Connector\Example\Utility\Mmc; @@ -18,7 +19,6 @@ use jtl\Connector\Model\Identity; use jtl\Connector\Model\Language; use jtl\Connector\Model\TaxRate; use jtl\Connector\Result\Action; -use jtl\Connector\Serializer\JMS\SerializerBuilder; class GlobalData extends DataController { @@ -90,4 +90,24 @@ class GlobalData extends DataController return $action; } + + /** + * Insert or update + * + * @param \jtl\Connector\Core\Model\DataModel $model + * @return \jtl\Connector\Result\Action + */ + public function push(DataModel $model) + { + $action = new Action(); + $action->setHandled(true); + + try { + $action->setResult(new GlobalData()); + } catch (\Exception $exc) { + $action->setError($this->handleException($exc)); + } + + return $action; + } } diff --git a/src/Mapper/DataMapper.php b/src/Mapper/DataMapper.php index 8f98c42199cde434439ff5312d2b735709bf5076..da38604dd5ba3ae6279f98a0b143e2a12ed00480 100644 --- a/src/Mapper/DataMapper.php +++ b/src/Mapper/DataMapper.php @@ -58,9 +58,24 @@ abstract class DataMapper extends Singleton { $json = $model->toJson(); $type = strtolower(ClassName::getFromNS(get_called_class())); - $id = $this->db->insert(sprintf('INSERT INTO %s (id, data) VALUES (null, %s)', $type, $json)); - $model->getId()->setEndpoint($id); + if ($model->getId()->getEndpoint() > 0) { + $stmt = $this->db->prepare(sprintf('UPDATE %s SET data = :data WHERE id = :id', $type)); + $stmt->bindValue(':table', $type, SQLITE3_TEXT); + $stmt->bindValue(':data', $json, SQLITE3_TEXT); + $stmt->bindValue(':id', $model->getId()->getEndpoint(), SQLITE3_INTEGER); + + $stmt->execute(); + } else { + $stmt = $this->db->prepare(sprintf('INSERT INTO %s (id, data) VALUES (null, :data)', $type)); + $stmt->bindValue(':table', $type, SQLITE3_TEXT); + $stmt->bindValue(':data', $json, SQLITE3_TEXT); + + $result = $stmt->execute(); + if ($result) { + $model->getId()->setEndpoint($this->db->getLastInsertRowId()); + } + } } public function remove($id)