]> git.mxchange.org Git - friendica.git/blobdiff - src/BaseModel.php
Improved order of systems
[friendica.git] / src / BaseModel.php
index f8cef0c13e707ee0d94b3971427d4271e027542a..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,13 +28,33 @@ abstract class BaseModel
         */
        private $data = [];
 
-       public function __construct(Database $dba, LoggerInterface $logger, $data = [])
+       /**
+        * @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;
+       }
+
        /**
         * Magic getter. This allows to retrieve model fields with the following syntax:
         * - $model->field (outside of class)
@@ -62,33 +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)
        {
-               $data = $this->dba->selectFirst(static::$table_name, [], $condition);
-
-               if (!$data) {
-                       throw new HTTPException\NotFoundException(static::class . ' record not found.');
-               }
-
-               return new static($this->dba, $this->logger, $data);
+               $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;
        }
 }