]> git.mxchange.org Git - friendica.git/blobdiff - src/BaseModel.php
Improved order of systems
[friendica.git] / src / BaseModel.php
index 32011c7db2f916189ab14245e3e16a1179b276df..055f9c4a19a46dd07c40ad4fa32a612c1a99e807 100644 (file)
@@ -7,8 +7,6 @@ use Friendica\Network\HTTPException;
 use Psr\Log\LoggerInterface;
 
 /**
- * Class BaseModel
- *
  * The Model classes inheriting from this abstract class are meant to represent a single database record.
  * The associated table name has to be provided in the child class, and the table is expected to have a unique `id` field.
  *
@@ -16,8 +14,6 @@ use Psr\Log\LoggerInterface;
  */
 abstract class BaseModel
 {
-       protected static $table_name;
-
        /** @var Database */
        protected $dba;
        /** @var LoggerInterface */
@@ -32,10 +28,31 @@ abstract class BaseModel
         */
        private $data = [];
 
-       public function __construct(Database $dba, LoggerInterface $logger)
+       /**
+        * @param Database        $dba
+        * @param LoggerInterface $logger
+        * @param array           $data   Table row attributes
+        */
+       public function __construct(Database $dba, LoggerInterface $logger, array $data = [])
        {
                $this->dba = $dba;
                $this->logger = $logger;
+               $this->data = $data;
+       }
+
+       /**
+        * Performance-improved model creation in a loop
+        *
+        * @param BaseModel $prototype
+        * @param array     $data
+        * @return BaseModel
+        */
+       public static function createFromPrototype(BaseModel $prototype, array $data)
+       {
+               $model = clone $prototype;
+               $model->data = $data;
+
+               return $model;
        }
 
        /**
@@ -61,35 +78,16 @@ abstract class BaseModel
        }
 
        /**
-        * Fetches a single model record. The condition array is expected to contain a unique index (primary or otherwise).
-        *
-        * Chainable.
-        *
-        * @param array $condition
-        * @return BaseModel
-        * @throws HTTPException\NotFoundException
+        * @param string $name
+        * @param mixed $value
         */
-       public function fetch(array $condition)
+       public function __set($name, $value)
        {
-               $intro = $this->dba->selectFirst(static::$table_name, [], $condition);
-
-               if (!$intro) {
-                       throw new HTTPException\NotFoundException(static::class . ' record not found.');
-               }
-
-               $this->data = $intro;
-
-               return $this;
+               $this->data[$name] = $value;
        }
 
-       /**
-        * Deletes the model record from the database.
-        * Prevents further methods from being called by wiping the internal model data.
-        */
-       public function delete()
+       public function toArray()
        {
-               if ($this->dba->delete(static::$table_name, ['id' => $this->id])) {
-                       $this->data = [];
-               }
+               return $this->data;
        }
 }