1: <?php
2: /*
3: * The MIT License
4: *
5: * Copyright 2017 David Schoenbauer.
6: *
7: * Permission is hereby granted, free of charge, to any person obtaining a copy
8: * of this software and associated documentation files (the "Software"), to deal
9: * in the Software without restriction, including without limitation the rights
10: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11: * copies of the Software, and to permit persons to whom the Software is
12: * furnished to do so, subject to the following conditions:
13: *
14: * The above copyright notice and this permission notice shall be included in
15: * all copies or substantial portions of the Software.
16: *
17: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23: * THE SOFTWARE.
24: */
25: namespace DSchoenbauer\Orm\Events\Persistence;
26:
27: use DSchoenbauer\Orm\Events\AbstractEvent;
28: use DSchoenbauer\Orm\Model;
29: use DSchoenbauer\Sql\Command\Select;
30: use DSchoenbauer\Sql\Where\ArrayWhere;
31: use PDO;
32: use Zend\EventManager\Event;
33:
34: /**
35: * Event driven hook to select information from a PDO connection
36: *
37: * @author David Schoenbauer
38: */
39: class PdoSelect extends AbstractEvent
40: {
41:
42: private $adapter;
43: private $select;
44:
45: /**
46: * @param array $events An array of event names to bind to
47: * @param PDO $adapter
48: * @param Select $select
49: */
50: public function __construct(array $events, \PDO $adapter, Select $select = null)
51: {
52: parent::__construct($events);
53: $this->setAdapter($adapter)
54: ->setSelect($select);
55: }
56:
57: /**
58: * event action
59: * @param Event $event object passed when event is fired
60: * @return void
61: * @since v1.0.0
62: */
63: public function onExecute(Event $event)
64: {
65: if (!$event->getTarget() instanceof Model) {
66: return; //Nothing to do with this event
67: }
68: /* @var $model Model */
69: $model = $event->getTarget();
70: $entity = $model->getEntity();
71: $model->setData(
72: $this->getSelect()
73: ->setTable($entity->getTable())
74: ->setFields($entity->getAllFields())
75: ->setWhere(new ArrayWhere([$entity->getIdField() => $model->getId()]))->setFetchFlat()
76: ->execute($this->getAdapter())
77: );
78: }
79:
80: /**
81: * Returns a PHP Data Object
82: * @return PDO
83: * @since v1.0.0
84: */
85: public function getAdapter()
86: {
87: return $this->adapter;
88: }
89:
90: /**
91: * PDO connection to a db of some sort.
92: * @param PDO $adapter
93: * @return $this
94: * @since v1.0.0
95: */
96: public function setAdapter(PDO $adapter)
97: {
98: $this->adapter = $adapter;
99: return $this;
100: }
101:
102: /**
103: * object with logic for the Select. If Select is not provided one will be lazy loaded
104: * @return Select
105: * @since v1.0.0
106: */
107: public function getSelect()
108: {
109: if (!$this->select instanceof Select) {
110: $this->select = new Select("");
111: }
112: return $this->select;
113: }
114:
115: /**
116: * Object that contains the select logic
117: * @param Select $select
118: * @return $this
119: * @since v1.0.0
120: */
121: public function setSelect(Select $select = null)
122: {
123: $this->select = $select;
124: return $this;
125: }
126: }
127: