5 use Friendica\Database\Database;
6 use Friendica\Network\HTTPException;
7 use Psr\Log\LoggerInterface;
10 * The Model classes inheriting from this abstract class are meant to represent a single database record.
11 * The associated table name has to be provided in the child class, and the table is expected to have a unique `id` field.
15 abstract class BaseModel
19 /** @var LoggerInterface */
23 * Model record abstraction.
24 * Child classes never have to interact directly with it.
25 * Please use the magic getter instead.
32 * Used to limit/avoid updates if no data was changed.
36 private $originalData = [];
39 * @param Database $dba
40 * @param LoggerInterface $logger
41 * @param array $data Table row attributes
43 public function __construct(Database $dba, LoggerInterface $logger, array $data = [])
46 $this->logger = $logger;
48 $this->originalData = $data;
51 public function getOriginalData()
53 return $this->originalData;
57 * Performance-improved model creation in a loop
59 * @param BaseModel $prototype
63 public static function createFromPrototype(BaseModel $prototype, array $data)
65 $model = clone $prototype;
67 $model->originalData = $data;
73 * Magic getter. This allows to retrieve model fields with the following syntax:
74 * - $model->field (outside of class)
75 * - $this->field (inside of class)
79 * @throws HTTPException\InternalServerErrorException
81 public function __get($name)
83 if (empty($this->data['id'])) {
84 throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');
87 if (!array_key_exists($name, $this->data)) {
88 throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
91 return $this->data[$name];
98 public function __set($name, $value)
100 $this->data[$name] = $value;
103 public function toArray()