-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModel.php
More file actions
199 lines (171 loc) · 4.49 KB
/
Model.php
File metadata and controls
199 lines (171 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace ipl\Orm;
use ipl\Orm\Common\PropertiesWithDefaults;
use ipl\Sql\Connection;
use ipl\Sql\ExpressionInterface;
/**
* Models represent single database tables or parts of it.
* They are also used to interact with the tables, i.e. in order to query for data.
*/
abstract class Model implements \ArrayAccess, \IteratorAggregate
{
use PropertiesWithDefaults;
private $relation;
public function setRelation(Relation $relation)
{
$this->relation = $relation;
}
private $nullableProperties;
final public function __construct(array $properties = null)
{
if ($this->hasProperties()) {
$this->setProperties($properties);
}
$this->init();
}
/**
* Get the related database table's name
*
* @return string
*/
abstract public function getTableName();
/**
* Get the column name(s) of the primary key
*
* @return string|array<string> Array if the primary key is compound, string otherwise
*/
abstract public function getKeyName();
/**
* Get the model's queryable columns
*
* @return array<int|string, string|ExpressionInterface>
*/
abstract public function getColumns();
/**
* Get the configured table alias. (Default {@see static::getTableName()})
*
* @return string
*/
public function getTableAlias(): string
{
return $this->getTableName();
}
/**
* Get the model's column definitions
*
* The array is indexed by column names, values are either strings (labels) or arrays of this format:
*
* [
* 'label' => 'A Column',
* 'type' => 'enum(y,n)'
* ]
*
* @return array
*/
public function getColumnDefinitions()
{
return [];
}
/**
* Get a query which is tied to this model and the given database connection
*
* @param Connection $db
*
* @return Query
*/
public static function on(Connection $db)
{
return (new Query())
->setDb($db)
->setModel(new static());
}
/**
* Get the model's default sort
*
* @return array|string
*/
public function getDefaultSort()
{
return [];
}
/**
* Get the model's search columns
*
* @return array
*/
public function getSearchColumns()
{
return [];
}
/**
* Create the model's behaviors
*
* @param Behaviors $behaviors
*/
public function createBehaviors(Behaviors $behaviors)
{
}
/**
* Create the model's defaults
*
* @param Defaults $defaults
*/
public function createDefaults(Defaults $defaults)
{
}
/**
* Create the model's relations
*
* If your model should be associated to other models, override this method and create the model's relations.
*/
public function createRelations(Relations $relations)
{
}
/**
* Initialize the model
*
* If you want to adjust the model after construction, override this method.
*/
protected function init()
{
}
private function nullableColumns()
{
if ($this->nullableProperties !== null) {
return $this->nullableProperties;
}
if ($this->relation === null) {
$this->nullableProperties = (function (): array {
$ref = new \ReflectionClass($this);
$doc = $ref->getDocComment();
preg_match_all('/@property\s+([^\s]+)\s+\$([^\s]+)/', $doc, $matches);
$cols = [];
foreach ($matches[1] as $i => $type) {
if (strpos($type, '?') === 0 || preg_match('~\|?null\|?~', $type)) {
$cols[] = $matches[2][$i];
}
}
return $cols;
})();
} elseif ($this->relation->getJoinType() === 'LEFT') {
$this->nullableProperties = array_merge($this->getColumns(), (array) $this->getKeyName());
} else {
$this->nullableProperties = [];
}
return $this->nullableProperties;
}
public function __get($key)
{
if (in_array($key, $this->nullableColumns(), true)) {
return null;
}
return $this->getProperty($key);
}
public function __isset($key)
{
if (in_array($key, $this->nullableColumns(), true)) {
return false;
}
return $this->hasProperty($key);
}
}