|
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
|
|
|
private $relationships = []; |
|
14
|
|
|
private $table; |
|
15
|
|
|
private $name; |
|
16
|
|
|
|
|
17
|
8 |
|
public function __construct($model) |
|
18
|
|
|
{ |
|
19
|
8 |
|
$this->table = $model->getTable(); |
|
20
|
8 |
|
$this->name = Nibii::getModelName((new \ReflectionClass($model))->getName()); |
|
21
|
8 |
|
$relationships = $model->getRelationships(); |
|
22
|
8 |
|
$adapter = DriverAdapter::getDefaultInstance(); |
|
23
|
8 |
|
$schema = Db::getDriver()->describeTable($this->table)[$this->table]; |
|
24
|
8 |
|
$this->autoPrimaryKey = $schema['auto_increment']; |
|
25
|
|
|
|
|
26
|
8 |
|
foreach ($schema['columns'] as $field => $details) { |
|
27
|
8 |
|
$this->fields[$field] = [ |
|
28
|
8 |
|
'type' => $adapter->mapDataTypes($details['type']), |
|
29
|
8 |
|
'required' => !$details['nulls'], |
|
30
|
8 |
|
'default' => $details['default'], |
|
31
|
8 |
|
'name' => $field |
|
32
|
|
|
]; |
|
33
|
8 |
|
if(isset($details['default'])) { |
|
34
|
4 |
|
$this->fields[$field]['default'] = $details['default']; |
|
35
|
|
|
} |
|
36
|
8 |
|
if(isset($details['length'])) { |
|
37
|
8 |
|
$this->fields[$field]['length'] = $details['length']; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
8 |
|
$this->appendConstraints($schema['primary_key'], $this->primaryKey, true); |
|
42
|
8 |
|
$this->appendConstraints($schema['unique_keys'], $this->uniqueKeys); |
|
43
|
|
|
|
|
44
|
8 |
|
foreach($relationships as $type => $relations) { |
|
45
|
8 |
|
$this->createRelationships($type, $relations); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
8 |
|
} |
|
49
|
|
|
|
|
50
|
8 |
|
private function appendConstraints($constraints, &$key, $flat = false) |
|
51
|
|
|
{ |
|
52
|
8 |
|
foreach ($constraints as $constraint) { |
|
53
|
8 |
|
if ($flat) { |
|
54
|
8 |
|
$key = $constraint['columns']; |
|
55
|
8 |
|
break; |
|
56
|
|
|
} else { |
|
57
|
4 |
|
$key[] = array( |
|
58
|
4 |
|
'fields' => $constraint['columns'] |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
8 |
|
} |
|
63
|
|
|
|
|
64
|
8 |
|
private function getRelationshipDetails($relationship) |
|
65
|
|
|
{ |
|
66
|
8 |
|
if(is_string($relationship)) |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
4 |
|
'model' => $relationship, |
|
70
|
4 |
|
'name' => $relationship, |
|
71
|
4 |
|
'foreign_key' => '', |
|
72
|
4 |
|
'local_key' => '' |
|
73
|
|
|
]; |
|
74
|
4 |
|
} else if (is_array($relationship)) { |
|
75
|
|
|
return [ |
|
76
|
4 |
|
'model' => $relationship[0], |
|
77
|
4 |
|
'name' => isset($relationship['as']) ? $relationship['as'] : $relationship[0], |
|
78
|
4 |
|
'foreign_key' => isset($relationship['foreign_key']) ? $relationship['foreign_key'] : '', |
|
79
|
4 |
|
'local_key' => isset($relationship['local_key']) ? $relationship['local_key'] : '' |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
8 |
|
private function createRelationships($type, $relationships) |
|
85
|
|
|
{ |
|
86
|
8 |
|
foreach($relationships as $relationship) |
|
87
|
|
|
{ |
|
88
|
8 |
|
$relationship = $this->getRelationshipDetails($relationship); |
|
89
|
8 |
|
$class = "\\ntentan\\nibii\\relationships\\{$type}Relationship"; |
|
90
|
8 |
|
$relationshipObject = new $class(); |
|
91
|
8 |
|
$relationshipObject->setOptions($relationship); |
|
92
|
8 |
|
$relationshipObject->setup($this->name, $this->table, $this->primaryKey); |
|
93
|
8 |
|
$this->relationships[$relationship['name']] = $relationshipObject; |
|
94
|
|
|
} |
|
95
|
8 |
|
} |
|
96
|
|
|
|
|
97
|
18 |
|
public function getRelationships() |
|
98
|
|
|
{ |
|
99
|
18 |
|
return $this->relationships; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
24 |
|
public function getPrimaryKey() |
|
103
|
|
|
{ |
|
104
|
24 |
|
return $this->primaryKey; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getAutoPrimaryKey() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->autoPrimaryKey; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getFields() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->fields; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function getUniqueKeys() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->uniqueKeys; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|