1
|
|
|
<?php |
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
|
|
|
* @package samsonframework\orm |
13
|
|
|
*/ |
14
|
|
|
class Manager |
15
|
|
|
{ |
16
|
|
|
/** @var string Entity identifier */ |
17
|
|
|
protected $entityName; |
18
|
|
|
|
19
|
|
|
/** @var string Entity primary field name */ |
20
|
|
|
protected $primaryFieldName; |
21
|
|
|
|
22
|
|
|
/** @var array Collection of entity field names and their types */ |
23
|
|
|
protected $attributes = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Manager constructor. |
27
|
|
|
* |
28
|
|
|
* @param string $entityName Entity name |
29
|
|
|
* @param array $attributes Key-value collection with field name => type |
30
|
|
|
* @param \PDO $driver database low-level driver |
31
|
|
|
*/ |
32
|
|
|
public function __construct($entityName, $attributes, $driver) |
33
|
|
|
{ |
34
|
|
|
$this->entityName = $entityName; |
35
|
|
|
$this->attributes = $attributes; |
36
|
|
|
$this->driver = $driver; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get new entity instance. |
41
|
|
|
* |
42
|
|
|
* @return RecordInterface New database manager entity instance |
43
|
|
|
*/ |
44
|
|
|
public function instance() |
45
|
|
|
{ |
46
|
|
|
return new $this->entityName($this); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Convert RecordInterface instance to collection of its field name => value. |
51
|
|
|
* |
52
|
|
|
* @param RecordInterface $object Database record instance to convert |
53
|
|
|
* @return array Collection of key => value with SQL fields statements |
54
|
|
|
*/ |
55
|
|
|
protected function &getFields(RecordInterface &$object = null) |
56
|
|
|
{ |
57
|
|
|
$collection = array(); |
58
|
|
|
foreach ($this->attributes as $attribute => $type) { |
59
|
|
|
if ($type == 'timestamp') { |
60
|
|
|
continue; |
61
|
|
|
} elseif ($this->primaryFieldName == $attribute) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$collection[$attribute] = $this->quote($object->$attribute); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $collection; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create new database entity record. |
73
|
|
|
* @param RecordInterface $entity Entity record for creation |
74
|
|
|
* @return RecordInterface Created database entity record with new primary identifier |
75
|
|
|
*/ |
76
|
|
|
public function create(RecordInterface $entity) |
77
|
|
|
{ |
78
|
|
|
$fields = $this->getFields($entity); |
79
|
|
|
|
80
|
|
|
$this->execute('INSERT INTO `' . $this->entityName . '` (`' |
|
|
|
|
81
|
|
|
. implode('`,`', array_keys($fields)) . '`) VALUES (' . implode(',', $fields) . ')' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Read database entity records from QueryInterface. |
87
|
|
|
* |
88
|
|
|
* @param QueryInterface $query For retrieving records |
89
|
|
|
* @return RecordInterface[] Collection of read database entity records |
90
|
|
|
*/ |
91
|
|
|
public function read(QueryInterface $query) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
// TODO: Implement read() method. |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Update database entity record. |
98
|
|
|
* |
99
|
|
|
* @param RecordInterface $entity Entity record for updating |
100
|
|
|
*/ |
101
|
|
|
public function update(RecordInterface $entity) |
102
|
|
|
{ |
103
|
|
|
// Generate entity fields update command |
104
|
|
|
$fields = array(); |
105
|
|
|
foreach ($this->getFields($entity) as $fieldName => $fieldValue) { |
106
|
|
|
$fields[] = '`'.$this->entityName.'`.`'.$fieldName.'` = "'.$fieldValue.'"'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->execute('UPDATE `' . $this->entityName . '` SET ' |
|
|
|
|
110
|
|
|
. implode(',', $fields) |
111
|
|
|
. ' WHERE `' . $this->entityName . '`.`' . $this->primaryFieldName . '`="' |
112
|
|
|
. $this->quote($entity->id) . '"'); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Delete database record from database. |
117
|
|
|
* |
118
|
|
|
* @param RecordInterface $entity Entity record for removing |
119
|
|
|
*/ |
120
|
|
|
public function delete(RecordInterface $entity) |
121
|
|
|
{ |
122
|
|
|
$this->execute('DELETE FROM `' . $this->entityName . '` WHERE ' |
|
|
|
|
123
|
|
|
. $this->primaryFieldName . ' = "' . $this->quote($entity->id) . '"' |
|
|
|
|
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: