]> git.mxchange.org Git - friendica.git/commitdiff
Enable Model lazy updating based on only the changed data
authorHypolite Petovan <hypolite@mrpetovan.com>
Tue, 14 Jan 2020 02:58:19 +0000 (21:58 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 20 Jan 2020 12:32:48 +0000 (07:32 -0500)
- Simplify update decision in Database

src/BaseModel.php
src/BaseRepository.php
src/Database/Database.php

index 055f9c4a19a46dd07c40ad4fa32a612c1a99e807..5a27089143d8ab48ff52cf3f4be5b43799a91ae1 100644 (file)
@@ -28,6 +28,13 @@ abstract class BaseModel
         */
        private $data = [];
 
+       /**
+        * Used to limit/avoid updates if no data was changed.
+        *
+        * @var array
+        */
+    private $originalData = [];
+
        /**
         * @param Database        $dba
         * @param LoggerInterface $logger
@@ -38,6 +45,12 @@ abstract class BaseModel
                $this->dba = $dba;
                $this->logger = $logger;
                $this->data = $data;
+               $this->originalData = $data;
+       }
+
+       public function getOriginalData()
+       {
+               return $this->originalData;
        }
 
        /**
@@ -51,6 +64,7 @@ abstract class BaseModel
        {
                $model = clone $prototype;
                $model->data = $data;
+               $model->originalData = $data;
 
                return $model;
        }
index 9f43d8fe144437adb62ee5c311667cb45833eb50..30822091e1784f8e63842c47a465ea753a7686b8 100644 (file)
@@ -122,7 +122,7 @@ abstract class BaseRepository extends BaseFactory
         */
        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());
        }
 
        /**
@@ -136,11 +136,13 @@ abstract class BaseRepository extends BaseFactory
        {
                $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);
+
                return $return;
        }
 
index 8a893ab9350c0aac19e6bc78c492d5790e99e789..14e186a72f63a4cbf94d31869da5b81e2f5d52eb 100644 (file)
@@ -1342,19 +1342,13 @@ class Database
                        }
                }
 
-               $do_update = (count($old_fields) == 0);
-
                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) {
                        return true;
                }