1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: VITALYIEGOROV |
5
|
|
|
* Date: 30.11.15 |
6
|
|
|
* Time: 16:38 |
7
|
|
|
*/ |
8
|
|
|
namespace samsonframework\orm; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Database entity manager. |
12
|
|
|
* |
13
|
|
|
* @author Vitaly Egorov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Manager |
16
|
|
|
{ |
17
|
|
|
/** @var string Entity identifier */ |
18
|
|
|
protected $entityName; |
19
|
|
|
|
20
|
|
|
/** @var string Entity primary field name */ |
21
|
|
|
protected $primaryFieldName; |
22
|
|
|
|
23
|
|
|
/** @var array Collection of entity fields that could be used in queries */ |
24
|
|
|
protected $queryFields = array(); |
25
|
|
|
|
26
|
|
|
/** @var array Collection of entity field names and their types */ |
27
|
|
|
protected $fieldsAndTypes = array(); |
28
|
|
|
|
29
|
|
|
/** @var DatabaseInterface Database manager */ |
30
|
|
|
protected $database; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Manager constructor. |
34
|
|
|
* |
35
|
|
|
* @param DatabaseInterface $database database low-level driver |
36
|
|
|
* @param string $entityName Entity name |
37
|
|
|
* @param array $attributes Key-value collection with field name => type |
38
|
|
|
*/ |
39
|
|
|
public function __construct(DatabaseInterface $database, string $entityName, $attributes) |
40
|
|
|
{ |
41
|
|
|
$this->database = $database; |
42
|
|
|
$this->entityName = $entityName; |
43
|
|
|
$this->fieldsAndTypes = $attributes; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Store entity into database. |
48
|
|
|
* |
49
|
|
|
* @param mixed $entity Database entity |
50
|
|
|
*/ |
51
|
|
|
public function save($entity) |
52
|
|
|
{ |
53
|
|
|
$fields = $this->getFields($entity); |
|
|
|
|
54
|
|
|
// |
55
|
|
|
// $this->execute('INSERT INTO `' . $this->entityName . '` (`' |
56
|
|
|
// . implode('`,`', array_keys($fields)) . '`) VALUES (' . implode(',', $fields) . ')' |
57
|
|
|
// ); |
58
|
|
|
|
59
|
|
|
// Generate entity fields update command |
60
|
|
|
// $fields = array(); |
61
|
|
|
// foreach ($this->getFields($entity) as $fieldName => $fieldValue) { |
62
|
|
|
// $fields[] = '`'.$this->entityName.'`.`'.$fieldName.'` = "'.$fieldValue.'"'; |
63
|
|
|
// } |
64
|
|
|
// |
65
|
|
|
// $this->execute('UPDATE `' . $this->entityName . '` SET ' |
66
|
|
|
// . implode(',', $fields) |
67
|
|
|
// . ' WHERE `' . $this->entityName . '`.`' . $this->primaryFieldName . '`="' |
68
|
|
|
// . $this->quote($entity->id) . '"'); |
69
|
|
|
|
70
|
|
|
//$this->database-> |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get new entity instance. |
75
|
|
|
* |
76
|
|
|
* @return mixed Entity instance |
77
|
|
|
*/ |
78
|
|
|
public function create() |
79
|
|
|
{ |
80
|
|
|
return new $this->entityName($this); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Convert RecordInterface instance to collection of its field name => value, |
85
|
|
|
* returning only fields that needs to participate in SQL statements. |
86
|
|
|
* TODO: We need to generate this collection in entity class generation. |
87
|
|
|
* |
88
|
|
|
* @param RecordInterface $object Database record instance to convert |
89
|
|
|
* @return array Collection of key => value with SQL fields statements |
90
|
|
|
*/ |
91
|
|
|
protected function &getQueryFields(RecordInterface &$object = null) |
92
|
|
|
{ |
93
|
|
|
$collection = array(); |
94
|
|
|
foreach ($this->fieldsAndTypes as $attribute => $type) { |
95
|
|
|
if ($type == 'timestamp') { |
96
|
|
|
continue; |
97
|
|
|
} elseif ($this->primaryFieldName == $attribute) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$collection[$attribute] = $object->$attribute; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $collection; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// /** |
108
|
|
|
// * Create new database entity record. |
109
|
|
|
// * @param RecordInterface $entity Entity record for creation |
110
|
|
|
// * @return RecordInterface Created database entity record with new primary identifier |
111
|
|
|
// */ |
112
|
|
|
// public function create(RecordInterface $entity) |
113
|
|
|
// { |
114
|
|
|
// $fields = $this->getFields($entity); |
115
|
|
|
// |
116
|
|
|
// $this->execute('INSERT INTO `' . $this->entityName . '` (`' |
117
|
|
|
// . implode('`,`', array_keys($fields)) . '`) VALUES (' . implode(',', $fields) . ')' |
118
|
|
|
// ); |
119
|
|
|
// } |
120
|
|
|
// |
121
|
|
|
// /** |
122
|
|
|
// * Read database entity records from QueryInterface. |
123
|
|
|
// * |
124
|
|
|
// * @param QueryInterface $query For retrieving records |
125
|
|
|
// * @return RecordInterface[] Collection of read database entity records |
126
|
|
|
// */ |
127
|
|
|
// public function read(QueryInterface $query) |
128
|
|
|
// { |
129
|
|
|
// // TODO: Implement read() method. |
130
|
|
|
// } |
131
|
|
|
// |
132
|
|
|
// /** |
133
|
|
|
// * Update database entity record. |
134
|
|
|
// * |
135
|
|
|
// * @param RecordInterface $entity Entity record for updating |
136
|
|
|
// */ |
137
|
|
|
// public function update(RecordInterface $entity) |
138
|
|
|
// { |
139
|
|
|
// // Generate entity fields update command |
140
|
|
|
// $fields = array(); |
141
|
|
|
// foreach ($this->getFields($entity) as $fieldName => $fieldValue) { |
142
|
|
|
// $fields[] = '`'.$this->entityName.'`.`'.$fieldName.'` = "'.$fieldValue.'"'; |
143
|
|
|
// } |
144
|
|
|
// |
145
|
|
|
// $this->execute('UPDATE `' . $this->entityName . '` SET ' |
146
|
|
|
// . implode(',', $fields) |
147
|
|
|
// . ' WHERE `' . $this->entityName . '`.`' . $this->primaryFieldName . '`="' |
148
|
|
|
// . $this->quote($entity->id) . '"'); |
149
|
|
|
// } |
150
|
|
|
// |
151
|
|
|
// /** |
152
|
|
|
// * Delete database record from database. |
153
|
|
|
// * |
154
|
|
|
// * @param RecordInterface $entity Entity record for removing |
155
|
|
|
// */ |
156
|
|
|
// public function delete(RecordInterface $entity) |
157
|
|
|
// { |
158
|
|
|
// $this->execute('DELETE FROM `' . $this->entityName . '` WHERE ' |
159
|
|
|
// . $this->primaryFieldName . ' = "' . $this->quote($entity->id) . '"' |
160
|
|
|
// ); |
161
|
|
|
// } |
162
|
|
|
} |
163
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.