Abstract.php
00001 <?php
00009 abstract class Dsao_Db_Table_DataHandler_Abstract
00010 extends Dsao_DataHandler_Abstract
00011 {
00018 protected $_identifiers = array();
00019
00026 protected $_synchronous = null;
00027
00031 protected $_table = null;
00032
00043 public function __construct(array $data = array(), $readOnly = false, $synchronous = false)
00044 {
00045 parent::__construct($data, $readOnly);
00046
00047 $this->setSynchronous($synchronous);
00048 }
00049
00057 public function __sleep()
00058 {
00059 return array_merge(parent::__sleep(), array('_synchronous'));
00060 }
00061
00079 protected function _addWhere(Zend_Db_Select $select, array $identifiers = array(), array $options = array())
00080 {
00081
00082 $table = (isset($options['table']) ? $options['table'].'.' : '');
00083 $values = (isset($options['values']) ? (array) $options['values'] : array());
00084
00085
00086 if (!$identifiers)
00087 {
00088 $identifiers = $this->_getValidIdentifiers();
00089 }
00090
00091
00092 $whereMethod = 'where';
00093
00094
00095 if (isset($options['operator']) && $operator == Zend_Db_Select::SQL_OR)
00096 {
00097 $whereMethod = 'orWhere';
00098 }
00099
00100
00101 foreach ($identifiers as $identifier)
00102 {
00103
00104 $identifierQuoted = $this->getTable()->getAdapter()
00105 ->quoteIdentifier($table.$identifier);
00106
00107
00108 $value = (isset($values[$identifier]) ? $values[$identifier] :
00109 $this->__get($identifier));
00110
00111
00112 $select->$whereMethod($identifierQuoted.' = ?', $value);
00113 }
00114 }
00115
00121 protected function _checkValidity()
00122 {
00123
00124
00125 if (parent::_checkValidity() &&
00126 ($this->hasIdentifier() || $this->isSynchronous()))
00127 {
00128 $this->_valid = true;
00129 }
00130
00131 return $this->_valid;
00132 }
00133
00141 protected function _getValidIdentifiers($firstOnly = true)
00142 {
00143 $identifiersValid = array();
00144
00145
00146 foreach ($this->_identifiers as $index => $identifiers)
00147 {
00148 $identifiers = (array) $identifiers;
00149
00150 foreach ($identifiers as $identifier)
00151 {
00152
00153
00154 if (!$this->__isset($identifier))
00155 {
00156 break 2;
00157 }
00158 }
00159
00160 $identifiersValid[] = $identifiers;
00161
00162
00163 if (true === $firstOnly)
00164 {
00165 return array_pop($identifiersValid);
00166 }
00167 }
00168
00169 return $identifiersValid;
00170 }
00171
00181 protected function _parseResult($row, $setSynchronous = true)
00182 {
00183 $synchronous = false;
00184
00185
00186 if ($row)
00187 {
00188 $synchronous = true;
00189
00190
00191 if (!is_array($row))
00192 {
00193 $row = $row->toArray();
00194 }
00195
00196
00197 $this->setData($row);
00198 }
00199
00200
00201 if ($setSynchronous)
00202 {
00203 $this->_synchronous = $synchronous;
00204 $this->_valid = true;
00205 }
00206
00207 return $synchronous;
00208 }
00209
00215 protected function _resetValidity()
00216 {
00217 parent::_resetValidity();
00218
00219
00220 if (true === $this->_synchronous)
00221 {
00222 $this->setSynchronous(false);
00223 }
00224 }
00225
00231 public function delete()
00232 {
00233
00234 $this->isReadOnly(true);
00235 $this->hasIdentifier(true);
00236
00237
00238 $this->_synchronous = null;
00239
00240 return $this;
00241 }
00242
00249 public function exists()
00250 {
00251
00252 if ($this->isSynchronous())
00253 {
00254 return true;
00255 }
00256
00257 else if (!$this->hasIdentifier())
00258 {
00259 return false;
00260 }
00261
00262 return null;
00263 }
00264
00273 public function getData($properties = null, $default = null, $reload = false)
00274 {
00275
00276 if ($reload)
00277 {
00278
00279 $this->refresh();
00280 }
00281
00282 return parent::getData($properties, $default);
00283 }
00284
00285
00292 public function getTable($name = null)
00293 {
00294
00295 if (null == $name)
00296 {
00297
00298 if (is_string($this->_table))
00299 {
00300 $this->_table = Dsao_Registry::getTable($this->_table);
00301 }
00302
00303 return $this->_table;
00304 }
00305
00306 else
00307 {
00308 return Dsao_Registry::getTable($name);
00309 }
00310 }
00311
00319 public function hasIdentifier($throw = false)
00320 {
00321
00322 if ($this->isSynchronous() || $this->_getValidIdentifiers())
00323 {
00324 return true;
00325 }
00326
00327 else if ($throw)
00328 {
00329 throw new Dsao_Exception
00330 ('dsao_db_table_datahandler_abstract_identifier');
00331 }
00332
00333 return false;
00334 }
00335
00341 public function insert()
00342 {
00343 $this->isReadOnly(true);
00344
00345
00346 if (true === $this->isSynchronous())
00347 {
00348 return $this;
00349 }
00350
00351 else if (false === $this->isSynchronous())
00352 {
00353 throw new Dsao_Exception
00354 ('dsao_db_table_datahandler_abstract_insert_synchronous');
00355 }
00356
00357
00358 $this->isValid(true);
00359
00360 return $this;
00361 }
00362
00370 public function isSynchronous($refresh = false)
00371 {
00372 if (false === $this->_synchronous && $refresh)
00373 {
00374 $this->refresh();
00375 }
00376
00377 return $this->_synchronous;
00378 }
00379
00386 public function refresh()
00387 {
00388
00389 $this->isReadOnly(true);
00390 $this->hasIdentifier(true);
00391
00392 return $this;
00393 }
00394
00401 public function setSynchronous($synchronous = true)
00402 {
00403 $this->_synchronous = (bool) $synchronous;
00404
00405
00406 if ($synchronous)
00407 {
00408 $this->_valid = true;
00409 }
00410
00411 return $this;
00412 }
00413
00420 public function update(array $properties = array())
00421 {
00422 $this->isReadOnly(true);
00423
00424
00425 if (null === $this->isSynchronous())
00426 {
00427 throw new Dsao_Exception
00428 ('dsao_db_table_datahandler_abstract_update_not_connected');
00429 }
00430
00431 return $this;
00432 }
00433 }