Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Db / RowGateway / AbstractRowGateway.php
blobd7589fff833900be91f4ee278b06ebb86b4286ee
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Db\RowGateway;
12 use ArrayAccess;
13 use Countable;
14 use Zend\Db\Sql\Sql;
15 use Zend\Db\Sql\TableIdentifier;
17 abstract class AbstractRowGateway implements ArrayAccess, Countable, RowGatewayInterface
20 /**
21 * @var bool
23 protected $isInitialized = false;
25 /**
26 * @var string|TableIdentifier
28 protected $table = null;
30 /**
31 * @var array
33 protected $primaryKeyColumn = null;
35 /**
36 * @var array
38 protected $primaryKeyData = null;
40 /**
41 * @var array
43 protected $data = array();
45 /**
46 * @var Sql
48 protected $sql = null;
50 /**
51 * @var Feature\FeatureSet
53 protected $featureSet = null;
55 /**
56 * initialize()
58 public function initialize()
60 if ($this->isInitialized) {
61 return;
64 if (!$this->featureSet instanceof Feature\FeatureSet) {
65 $this->featureSet = new Feature\FeatureSet;
68 $this->featureSet->setRowGateway($this);
69 $this->featureSet->apply('preInitialize', array());
71 if (!is_string($this->table) && !$this->table instanceof TableIdentifier) {
72 throw new Exception\RuntimeException('This row object does not have a valid table set.');
75 if ($this->primaryKeyColumn == null) {
76 throw new Exception\RuntimeException('This row object does not have a primary key column set.');
77 } elseif (is_string($this->primaryKeyColumn)) {
78 $this->primaryKeyColumn = (array) $this->primaryKeyColumn;
81 if (!$this->sql instanceof Sql) {
82 throw new Exception\RuntimeException('This row object does not have a Sql object set.');
85 $this->featureSet->apply('postInitialize', array());
87 $this->isInitialized = true;
90 /**
91 * Populate Data
93 * @param array $rowData
94 * @param bool $rowExistsInDatabase
95 * @return AbstractRowGateway
97 public function populate(array $rowData, $rowExistsInDatabase = false)
99 $this->initialize();
101 $this->data = $rowData;
102 if ($rowExistsInDatabase == true) {
103 $this->processPrimaryKeyData();
104 } else {
105 $this->primaryKeyData = null;
108 return $this;
112 * @param mixed $array
113 * @return array|void
115 public function exchangeArray($array)
117 return $this->populate($array, true);
121 * Save
123 * @return int
125 public function save()
127 $this->initialize();
129 if ($this->rowExistsInDatabase()) {
131 // UPDATE
133 $data = $this->data;
134 $where = array();
136 // primary key is always an array even if its a single column
137 foreach ($this->primaryKeyColumn as $pkColumn) {
138 $where[$pkColumn] = $this->primaryKeyData[$pkColumn];
139 if ($data[$pkColumn] == $this->primaryKeyData[$pkColumn]) {
140 unset($data[$pkColumn]);
144 $statement = $this->sql->prepareStatementForSqlObject($this->sql->update()->set($data)->where($where));
145 $result = $statement->execute();
146 $rowsAffected = $result->getAffectedRows();
147 unset($statement, $result); // cleanup
149 } else {
151 // INSERT
152 $insert = $this->sql->insert();
153 $insert->values($this->data);
155 $statement = $this->sql->prepareStatementForSqlObject($insert);
157 $result = $statement->execute();
158 if (($primaryKeyValue = $result->getGeneratedValue()) && count($this->primaryKeyColumn) == 1) {
159 $this->primaryKeyData = array($this->primaryKeyColumn[0] => $primaryKeyValue);
160 } else {
161 // make primary key data available so that $where can be complete
162 $this->processPrimaryKeyData();
164 $rowsAffected = $result->getAffectedRows();
165 unset($statement, $result); // cleanup
167 $where = array();
168 // primary key is always an array even if its a single column
169 foreach ($this->primaryKeyColumn as $pkColumn) {
170 $where[$pkColumn] = $this->primaryKeyData[$pkColumn];
175 // refresh data
176 $statement = $this->sql->prepareStatementForSqlObject($this->sql->select()->where($where));
177 $result = $statement->execute();
178 $rowData = $result->current();
179 unset($statement, $result); // cleanup
181 // make sure data and original data are in sync after save
182 $this->populate($rowData, true);
184 // return rows affected
185 return $rowsAffected;
189 * Delete
191 * @return int
193 public function delete()
195 $this->initialize();
197 $where = array();
198 // primary key is always an array even if its a single column
199 foreach ($this->primaryKeyColumn as $pkColumn) {
200 $where[$pkColumn] = $this->primaryKeyData[$pkColumn];
203 // @todo determine if we need to do a select to ensure 1 row will be affected
205 $statement = $this->sql->prepareStatementForSqlObject($this->sql->delete()->where($where));
206 $result = $statement->execute();
208 $affectedRows = $result->getAffectedRows();
209 if ($affectedRows == 1) {
210 // detach from database
211 $this->primaryKeyData = null;
214 return $affectedRows;
218 * Offset Exists
220 * @param string $offset
221 * @return bool
223 public function offsetExists($offset)
225 return array_key_exists($offset, $this->data);
229 * Offset get
231 * @param string $offset
232 * @return mixed
234 public function offsetGet($offset)
236 return $this->data[$offset];
240 * Offset set
242 * @param string $offset
243 * @param mixed $value
244 * @return RowGateway
246 public function offsetSet($offset, $value)
248 $this->data[$offset] = $value;
249 return $this;
253 * Offset unset
255 * @param string $offset
256 * @return AbstractRowGateway
258 public function offsetUnset($offset)
260 $this->data[$offset] = null;
261 return $this;
265 * @return int
267 public function count()
269 return count($this->data);
273 * To array
275 * @return array
277 public function toArray()
279 return $this->data;
283 * __get
285 * @param string $name
286 * @throws Exception\InvalidArgumentException
287 * @return mixed
289 public function __get($name)
291 if (array_key_exists($name, $this->data)) {
292 return $this->data[$name];
293 } else {
294 throw new Exception\InvalidArgumentException('Not a valid column in this row: ' . $name);
299 * __set
301 * @param string $name
302 * @param mixed $value
303 * @return void
305 public function __set($name, $value)
307 $this->offsetSet($name, $value);
311 * __isset
313 * @param string $name
314 * @return bool
316 public function __isset($name)
318 return $this->offsetExists($name);
322 * __unset
324 * @param string $name
325 * @return void
327 public function __unset($name)
329 $this->offsetUnset($name);
333 * @return bool
335 public function rowExistsInDatabase()
337 return ($this->primaryKeyData !== null);
341 * @throws Exception\RuntimeException
343 protected function processPrimaryKeyData()
345 $this->primaryKeyData = array();
346 foreach ($this->primaryKeyColumn as $column) {
347 if (!isset($this->data[$column])) {
348 throw new Exception\RuntimeException('While processing primary key data, a known key ' . $column . ' was not found in the data array');
350 $this->primaryKeyData[$column] = $this->data[$column];