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
17 protected static $table_name;
21 /** @var LoggerInterface */
25 * Model record abstraction.
26 * Child classes never have to interact directly with it.
27 * Please use the magic getter instead.
34 * @param Database $dba
35 * @param LoggerInterface $logger
36 * @param array $data Table row attributes
38 public function __construct(Database $dba, LoggerInterface $logger, array $data = [])
41 $this->logger = $logger;
46 * Performance-improved model creation in a loop
48 * @param BaseModel $prototype
52 public static function createFromPrototype(BaseModel $prototype, array $data)
54 $model = clone $prototype;
61 * Magic getter. This allows to retrieve model fields with the following syntax:
62 * - $model->field (outside of class)
63 * - $this->field (inside of class)
67 * @throws HTTPException\InternalServerErrorException
69 public function __get($name)
71 if (empty($this->data['id'])) {
72 throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');
75 if (!array_key_exists($name, $this->data)) {
76 throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
79 return $this->data[$name];
86 public function __set($name, $value)
88 $this->data[$name] = $value;
91 public function toArray()