1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
4
|
|
|
|
5
|
|
|
use ntentan\atiaa\Db; |
6
|
|
|
|
7
|
|
|
class ModelDescription { |
8
|
|
|
|
9
|
|
|
private $fields = []; |
10
|
|
|
private $primaryKey = []; |
11
|
|
|
private $uniqueKeys = []; |
12
|
|
|
private $autoPrimaryKey = false; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* @var array<Relationship> |
17
|
|
|
*/ |
18
|
|
|
private $relationships = []; |
19
|
|
|
private $table; |
20
|
|
|
private $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @param RecordWrapper $model |
25
|
|
|
*/ |
26
|
28 |
|
public function __construct($model) { |
27
|
28 |
|
$this->table = $model->getDBStoreInformation()['unquoted_table']; |
28
|
28 |
|
$this->name = Nibii::getModelName((new \ReflectionClass($model))->getName()); |
29
|
28 |
|
$relationships = $model->getRelationships(); |
30
|
28 |
|
$adapter = DriverAdapter::getDefaultInstance(); |
31
|
28 |
|
$schema = array_values(Db::getDriver()->describeTable($this->table))[0]; |
|
|
|
|
32
|
28 |
|
$this->autoPrimaryKey = $schema['auto_increment']; |
33
|
|
|
|
34
|
28 |
|
foreach ($schema['columns'] as $field => $details) { |
35
|
28 |
|
$this->fields[$field] = [ |
36
|
28 |
|
'type' => $adapter->mapDataTypes($details['type']), |
37
|
28 |
|
'required' => !$details['nulls'], |
38
|
28 |
|
'default' => $details['default'], |
39
|
28 |
|
'name' => $field |
40
|
|
|
]; |
41
|
28 |
|
if (isset($details['default'])) { |
42
|
20 |
|
$this->fields[$field]['default'] = $details['default']; |
43
|
|
|
} |
44
|
28 |
|
if (isset($details['length'])) { |
45
|
28 |
|
$this->fields[$field]['length'] = $details['length']; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
28 |
|
$this->appendConstraints($schema['primary_key'], $this->primaryKey, true); |
50
|
28 |
|
$this->appendConstraints($schema['unique_keys'], $this->uniqueKeys); |
51
|
|
|
|
52
|
28 |
|
foreach ($relationships as $type => $relations) { |
53
|
28 |
|
$this->createRelationships($type, $relations); |
54
|
|
|
} |
55
|
28 |
|
} |
56
|
|
|
|
57
|
28 |
|
private function appendConstraints($constraints, &$key, $flat = false) { |
58
|
28 |
|
foreach ($constraints as $constraint) { |
59
|
28 |
|
if ($flat) { |
60
|
28 |
|
$key = $constraint['columns']; |
61
|
28 |
|
break; |
62
|
|
|
} else { |
63
|
16 |
|
$key[] = array( |
64
|
16 |
|
'fields' => $constraint['columns'] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
28 |
|
} |
69
|
|
|
|
70
|
28 |
|
private function getRelationshipDetails($relationship) { |
71
|
28 |
|
$relationshipDetails = []; |
|
|
|
|
72
|
28 |
|
if (is_string($relationship)) { |
73
|
|
|
$relationshipDetails = [ |
74
|
22 |
|
'model' => $relationship, |
75
|
22 |
|
'name' => $relationship, |
76
|
22 |
|
'foreign_key' => '', |
77
|
22 |
|
'local_key' => '' |
78
|
|
|
]; |
79
|
6 |
|
} else if (is_array($relationship)) { |
80
|
|
|
$relationshipDetails = [ |
81
|
6 |
|
'model' => $relationship[0], |
82
|
6 |
|
'name' => isset($relationship['as']) ? $relationship['as'] : $relationship[0], |
83
|
6 |
|
'foreign_key' => isset($relationship['foreign_key']) ? $relationship['foreign_key'] : '', |
84
|
6 |
|
'local_key' => isset($relationship['local_key']) ? $relationship['local_key'] : '' |
85
|
|
|
]; |
86
|
|
|
} else { |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
28 |
|
$relationshipDetails['local_table'] = $this->table; |
90
|
28 |
|
return $relationshipDetails; |
91
|
|
|
} |
92
|
|
|
|
93
|
28 |
|
private function createRelationships($type, $relationships) { |
94
|
28 |
|
foreach ($relationships as $relationship) { |
95
|
28 |
|
$relationship = $this->getRelationshipDetails($relationship); |
96
|
28 |
|
$class = "\\ntentan\\nibii\\relationships\\{$type}Relationship"; |
97
|
28 |
|
$relationshipObject = new $class(); |
98
|
28 |
|
$relationshipObject->setOptions($relationship); |
99
|
28 |
|
$relationshipObject->setup($this->name, $this->table, $this->primaryKey); |
100
|
28 |
|
$this->relationships[$relationship['name']] = $relationshipObject; |
101
|
|
|
} |
102
|
28 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
* @return array<Relationship> |
107
|
|
|
*/ |
108
|
18 |
|
public function getRelationships() { |
109
|
18 |
|
return $this->relationships; |
110
|
|
|
} |
111
|
|
|
|
112
|
24 |
|
public function getPrimaryKey() { |
113
|
24 |
|
return $this->primaryKey; |
114
|
|
|
} |
115
|
|
|
|
116
|
10 |
|
public function getAutoPrimaryKey() { |
117
|
10 |
|
return $this->autoPrimaryKey; |
118
|
|
|
} |
119
|
|
|
|
120
|
10 |
|
public function getFields() { |
121
|
10 |
|
return $this->fields; |
122
|
|
|
} |
123
|
|
|
|
124
|
10 |
|
public function getUniqueKeys() { |
125
|
10 |
|
return $this->uniqueKeys; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: