Debian Codegraph @

vendor/ipl/orm/src/Model.php

raw
1 <?php
2
3 namespace ipl\Orm;
4
5 use ipl\Orm\Common\PropertiesWithDefaults;
6 use ipl\Sql\Connection;
7 use ipl\Sql\ExpressionInterface;
8
9 /**
10 * Models represent single database tables or parts of it.
11 * They are also used to interact with the tables, i.e. in order to query for data.
12 */
13 abstract class Model implements \ArrayAccess, \IteratorAggregate
14 {
15 use PropertiesWithDefaults;
16
17 final public function __construct(?array $properties = null)
18 {
19 if (! empty($properties)) {
20 $this->setProperties($properties);
21 }
22
23 $this->init();
24 }
25
26 /**
27 * Get the related database table's name
28 *
29 * @return string
30 */
31 abstract public function getTableName();
32
33 /**
34 * Get the column name(s) of the primary key
35 *
36 * @return string|array<string> Array if the primary key is compound, string otherwise
37 */
38 abstract public function getKeyName();
39
40 /**
41 * Get the model's queryable columns
42 *
43 * @return array<int|string, string|ExpressionInterface>
44 */
45 abstract public function getColumns();
46
47 /**
48 * Get the configured table alias. (Default {@see static::getTableName()})
49 *
50 * @return string
51 */
52 public function getTableAlias(): string
53 {
54 return $this->getTableName();
55 }
56
57 /**
58 * Get the model's column definitions
59 *
60 * The array is indexed by column names, values are either strings (labels) or arrays of this format:
61 *
62 * [
63 * 'label' => 'A Column',
64 * 'type' => 'enum(y,n)'
65 * ]
66 *
67 * @return array
68 */
69 public function getColumnDefinitions()
70 {
71 return [];
72 }
73
74 /**
75 * Get a query which is tied to this model and the given database connection
76 *
77 * @param Connection $db
78 *
79 * @return Query
80 */
81 public static function on(Connection $db)
82 {
83 return (new Query())
84 ->setDb($db)
85 ->setModel(new static());
86 }
87
88 /**
89 * Get the model's default sort
90 *
91 * @return array|string
92 */
93 public function getDefaultSort()
94 {
95 return [];
96 }
97
98 /**
99 * Get the model's search columns
100 *
101 * @return array
102 */
103 public function getSearchColumns()
104 {
105 return [];
106 }
107
108 /**
109 * Create the model's behaviors
110 *
111 * @param Behaviors $behaviors
112 */
113 public function createBehaviors(Behaviors $behaviors)
114 {
115 }
116
117 /**
118 * Create the model's defaults
119 *
120 * @param Defaults $defaults
121 */
122 public function createDefaults(Defaults $defaults)
123 {
124 }
125
126 /**
127 * Create the model's relations
128 *
129 * If your model should be associated to other models, override this method and create the model's relations.
130 */
131 public function createRelations(Relations $relations)
132 {
133 }
134
135 /**
136 * Initialize the model
137 *
138 * If you want to adjust the model after construction, override this method.
139 */
140 protected function init()
141 {
142 }
143 }