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 * @param Database $dba
33 * @param LoggerInterface $logger
34 * @param array $data Table row attributes
36 public function __construct(Database $dba, LoggerInterface $logger, array $data = [])
39 $this->logger = $logger;
44 * Performance-improved model creation in a loop
46 * @param BaseModel $prototype
50 public static function createFromPrototype(BaseModel $prototype, array $data)
52 $model = clone $prototype;
59 * Magic getter. This allows to retrieve model fields with the following syntax:
60 * - $model->field (outside of class)
61 * - $this->field (inside of class)
65 * @throws HTTPException\InternalServerErrorException
67 public function __get($name)
69 if (empty($this->data['id'])) {
70 throw new HTTPException\InternalServerErrorException(static::class . ' record uninitialized');
73 if (!array_key_exists($name, $this->data)) {
74 throw new HTTPException\InternalServerErrorException('Field ' . $name . ' not found in ' . static::class);
77 return $this->data[$name];
84 public function __set($name, $value)
86 $this->data[$name] = $value;
89 public function toArray()