1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Percy\Store; |
4
|
|
|
|
5
|
|
|
use Aura\Filter\FilterFactory; |
6
|
|
|
use Aura\SqlQuery\QueryFactory; |
7
|
|
|
use Aura\Sql\ExtendedPdo; |
8
|
|
|
use PDOException; |
9
|
|
|
use Percy\Entity\Collection; |
10
|
|
|
use Percy\Entity\EntityInterface; |
11
|
|
|
|
12
|
|
|
class SqlStore extends AbstractStore |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Aura\Sql\ExtendedPdo |
16
|
|
|
*/ |
17
|
|
|
protected $dbal; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Construct. |
21
|
|
|
* |
22
|
|
|
* @param \Aura\Sql\ExtendedPdo $dbal |
23
|
|
|
* @param \Aura\Filter\FilterFactory $filter |
24
|
|
|
*/ |
25
|
|
|
public function __construct(ExtendedPdo $dbal, FilterFactory $filter) |
26
|
|
|
{ |
27
|
|
|
$this->dbal = $dbal; |
28
|
|
|
$this->query = new QueryFactory('mysql'); |
|
|
|
|
29
|
|
|
parent::__construct($filter); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function create(Collection $collection, array $scopes = []) |
36
|
|
|
{ |
37
|
|
|
$this->validate($collection); |
38
|
|
|
$this->decorate($collection, StoreInterface::ON_CREATE); |
39
|
|
|
return $this->collectionIterator($collection, 'insertEntity'); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Insert an entity to the database. |
44
|
|
|
* |
45
|
|
|
* @param \Percy\Entity\EntityInterface $entity |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
protected function insertEntity(EntityInterface $entity) |
50
|
|
|
{ |
51
|
|
|
$insert = $this->query->newInsert(); |
52
|
|
|
$insert->into($entity->getDataSource()); |
53
|
|
|
$insert->cols($entity->toArray(false)); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$this->dbal->perform($insert->getStatement(), $insert->getBindValues()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function read(Collection $collection, array $scopes = []) |
62
|
|
|
{ |
63
|
|
|
// mysql need do nothing with the data on a read request |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function update(Collection $collection, array $scopes = []) |
71
|
|
|
{ |
72
|
|
|
$this->validate($collection); |
73
|
|
|
$this->decorate($collection, StoreInterface::ON_UPDATE); |
74
|
|
|
return $this->collectionIterator($collection, 'updateEntity'); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Update an entity in the database. |
79
|
|
|
* |
80
|
|
|
* @param \Percy\Entity\EntityInterface $entity |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
protected function updateEntity(EntityInterface $entity) |
85
|
|
|
{ |
86
|
|
|
$update = $this->query->newUpdate(); |
87
|
|
|
$update->table($entity->getDataSource()); |
88
|
|
|
$update->cols($entity->toArray(false)); |
|
|
|
|
89
|
|
|
$update->where(sprintf('%s = ?', $entity->getPrimary()), $entity[$entity->getPrimary()]); |
90
|
|
|
|
91
|
|
|
$this->dbal->perform($update->getStatement(), $update->getBindValues()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function delete(Collection $collection, array $scopes = []) |
98
|
|
|
{ |
99
|
|
|
$this->validate($collection); |
100
|
|
|
$this->decorate($collection, StoreInterface::ON_DELETE); |
101
|
|
|
return $this->collectionIterator($collection, 'deleteEntity'); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Delete an entity from the database. Be aware, this handles hard deletes. |
106
|
|
|
* To handle soft deletes, extend this class and overload this method. |
107
|
|
|
* |
108
|
|
|
* @param \Percy\Entity\EntityInterface $entity |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
protected function deleteEntity(EntityInterface $entity) |
113
|
|
|
{ |
114
|
|
|
$delete = $this->query->newDelete(); |
115
|
|
|
$delete->from($entity->getDataSource()); |
116
|
|
|
$delete->where(sprintf('%s = ?', $entity->getPrimary()), $entity[$entity->getPrimary()]); |
117
|
|
|
|
118
|
|
|
$this->dbal->perform($delete->getStatement(), $delete->getBindValues()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Iterate a collection with the correct callback. |
123
|
|
|
* |
124
|
|
|
* @param \Percy\Entity\Collection $collection |
125
|
|
|
* @param string $callable |
126
|
|
|
* |
127
|
|
|
* @return boolean |
128
|
|
|
*/ |
129
|
|
|
protected function collectionIterator(EntityCollection $collection, $callable) |
130
|
|
|
{ |
131
|
|
|
$this->dbal->beginEntity(); |
|
|
|
|
132
|
|
|
|
133
|
|
|
try { |
134
|
|
|
foreach ($collection->getIterator() as $entity) { |
135
|
|
|
call_user_func_array([$this, $callable], [$entity]); |
136
|
|
|
} |
137
|
|
|
} catch (PDOException $e) { |
138
|
|
|
$this->dbal->rollBack(); |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->dbal->commit(); |
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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: