1: <?php
2: 3: 4: 5: 6:
7: namespace DSchoenbauer\Orm\Events\Persistence;
8:
9: use DSchoenbauer\Orm\Events\AbstractEvent;
10: use DSchoenbauer\Orm\Model;
11: use DSchoenbauer\Sql\Command\Update;
12: use DSchoenbauer\Sql\Where\ArrayWhere;
13: use PDO;
14: use Zend\EventManager\Event;
15:
16: 17: 18: 19: 20:
21: class PdoUpdate extends AbstractEvent
22: {
23:
24: private $adapter;
25: private $update;
26:
27: 28: 29: 30: 31:
32: public function __construct(array $events, PDO $adapter, Update $update = null)
33: {
34: parent::__construct($events);
35: $this->setAdapter($adapter)->setUpdate($update);
36: }
37:
38: 39: 40: 41: 42: 43:
44: public function onExecute(Event $event)
45: {
46: if (!$event->getTarget() instanceof Model) {
47: return;
48: }
49:
50: $model = $event->getTarget();
51: $entity = $model->getEntity();
52: $this->getUpdate()
53: ->setTable($entity->getTable())->setData($model->getData())
54: ->setWhere(new ArrayWhere([$entity->getIdField() => $model->getId()]))
55: ->execute($this->getAdapter());
56: }
57:
58: 59: 60: 61: 62:
63: public function getAdapter()
64: {
65: return $this->adapter;
66: }
67:
68: 69: 70: 71: 72: 73:
74: public function setAdapter($adapter)
75: {
76: $this->adapter = $adapter;
77: return $this;
78: }
79:
80: 81: 82: 83: 84:
85: public function getUpdate()
86: {
87: if (!$this->update instanceof Update) {
88: $this->setUpdate(new Update(null, []));
89: }
90: return $this->update;
91: }
92:
93: 94: 95: 96: 97: 98:
99: public function setUpdate(Update $update = null)
100: {
101: $this->update = $update;
102: return $this;
103: }
104: }
105: