-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpdateOne.php
More file actions
65 lines (49 loc) · 1.66 KB
/
UpdateOne.php
File metadata and controls
65 lines (49 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace UniMapper\Query;
use UniMapper\Exception,
UniMapper\Entity\Reflection;
class UpdateOne extends \UniMapper\Query
{
/** @var \UniMapper\Entity */
protected $entity;
/** @var mixed */
protected $primaryValue;
public function __construct(
Reflection $reflection,
$primaryValue,
array $data
) {
parent::__construct($reflection);
$this->primaryValue = $primaryValue;
// Primary value update is not allowed
if (!$reflection->hasPrimary()) {
throw new Exception\QueryException(
"Entity '" . $reflection->getClassName() . "' has no "
. "primary property!"
);
}
// Do not change primary value
unset($data[$reflection->getPrimaryProperty()->getName()]);
$this->entity = $reflection->createEntity($data);
}
protected function onExecute(\UniMapper\Connection $connection)
{
$adapter = $connection->getAdapter($this->reflection->getAdapterName());
$mapper = $connection->getMapper();
$values = $mapper->unmapEntity($this->entity);
// Values can not be empty
if (empty($values)) {
throw new Exception\QueryException("Nothing to update!");
}
$query = $adapter->createUpdateOne(
$this->reflection->getAdapterResource(),
$this->reflection->getPrimaryProperty()->getUnmapped(),
$mapper->unmapValue(
$this->reflection->getPrimaryProperty(),
$this->primaryValue
),
$values
);
return (bool) $adapter->execute($query);
}
}