- Simplify update decision in Database
+ /**
+ * Used to limit/avoid updates if no data was changed.
+ *
+ * @var array
+ */
+ private $originalData = [];
+
/**
* @param Database $dba
* @param LoggerInterface $logger
/**
* @param Database $dba
* @param LoggerInterface $logger
$this->dba = $dba;
$this->logger = $logger;
$this->data = $data;
$this->dba = $dba;
$this->logger = $logger;
$this->data = $data;
+ $this->originalData = $data;
+ }
+
+ public function getOriginalData()
+ {
+ return $this->originalData;
{
$model = clone $prototype;
$model->data = $data;
{
$model = clone $prototype;
$model->data = $data;
+ $model->originalData = $data;
*/
public function update(BaseModel $model)
{
*/
public function update(BaseModel $model)
{
- return $this->dba->update(static::$table_name, $model->toArray(), ['id' => $model->id], true);
+ return $this->dba->update(static::$table_name, $model->toArray(), ['id' => $model->id], $model->getOriginalData());
{
$return = $this->dba->insert(static::$table_name, $fields);
{
$return = $this->dba->insert(static::$table_name, $fields);
- if ($return) {
- $fields['id'] = $this->dba->lastInsertId();
- $return = $this->create($fields);
+ if (!$return) {
+ throw new HTTPException\InternalServerErrorException('Unable to insert new row in table "' . static::$table_name . '"');
+ $fields['id'] = $this->dba->lastInsertId();
+ $return = $this->create($fields);
+
- $do_update = (count($old_fields) == 0);
-
foreach ($old_fields AS $fieldname => $content) {
foreach ($old_fields AS $fieldname => $content) {
- if (isset($fields[$fieldname])) {
- if (($fields[$fieldname] == $content) && !is_null($content)) {
- unset($fields[$fieldname]);
- } else {
- $do_update = true;
- }
+ if (isset($fields[$fieldname]) && !is_null($content) && ($fields[$fieldname] == $content)) {
+ unset($fields[$fieldname]);
- if (!$do_update || (count($fields) == 0)) {
+ if (count($fields) == 0) {