3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 use Friendica\Database\Database;
25 use Friendica\Network\HTTPException;
26 use Psr\Log\LoggerInterface;
29 * The Model classes inheriting from this abstract class are meant to represent a single database record.
30 * The associated table name has to be provided in the child class, and the table is expected to have a unique `id` field.
34 abstract class BaseModel extends BaseDataTransferObject
38 /** @var LoggerInterface */
42 * Model record abstraction.
43 * Child classes never have to interact directly with it.
44 * Please use the magic getter instead.
51 * Used to limit/avoid updates if no data was changed.
55 private $originalData = [];
58 * @param Database $dba
59 * @param LoggerInterface $logger
60 * @param array $data Table row attributes
62 public function __construct(Database $dba, LoggerInterface $logger, array $data = [])
65 $this->logger = $logger;
67 $this->originalData = $data;
70 public function getOriginalData(): array
72 return $this->originalData;
75 public function resetOriginalData()
77 $this->originalData = $this->data;
81 * Performance-improved model creation in a loop
83 * @param BaseModel $prototype
87 public static function createFromPrototype(BaseModel $prototype, array $data): BaseModel
89 $model = clone $prototype;
91 $model->originalData = $data;
97 * Magic isset method. Returns true if the field exists, either in the data prperty array or in any of the local properties.
98 * Used by array_column() on an array of objects.
103 public function __isset($name): bool
105 return in_array($name, array_merge(array_keys($this->data), array_keys(get_object_vars($this))));
109 * Magic getter. This allows to retrieve model fields with the following syntax:
110 * - $model->field (outside of class)
111 * - $this->field (inside of class)
113 * @param string $name Name of data to fetch
115 * @throws HTTPException\InternalServerErrorException
117 public function __get(string $name)
121 if (!array_key_exists($name, $this->data)) {
122 throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
125 return $this->data[$name];
129 * * Magic setter. This allows to set model fields with the following syntax:
130 * - $model->field = $value (outside of class)
131 * - $this->field = $value (inside of class)
133 * @param string $name
134 * @param mixed $value
136 public function __set(string $name, $value)
138 $this->data[$name] = $value;
141 public function toArray(): array
146 protected function checkValid()
148 if (!isset($this->data['id']) || is_null($this->data['id'])) {
149 throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');