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     // Optionen
00082     $table = (isset($options['table']) ? $options['table'].'.' : '');
00083     $values = (isset($options['values']) ? (array) $options['values'] : array());
00084 
00085     // Falls keine Identifizierer übergeben wurden, die ersten gültigen nehmen
00086     if (!$identifiers)
00087     {
00088       $identifiers = $this->_getValidIdentifiers();
00089     }
00090 
00091     // Methode zum Hinzufügen der Where-Klausel ermitteln
00092     $whereMethod = 'where';
00093 
00094     // Falls Operator übergeben wurde
00095     if (isset($options['operator']) && $operator == Zend_Db_Select::SQL_OR)
00096     {
00097       $whereMethod = 'orWhere';
00098     }
00099 
00100     // Identifizierer durchgehen
00101     foreach ($identifiers as $identifier)
00102     {
00103       // Identifizierer quoten
00104       $identifierQuoted = $this->getTable()->getAdapter()
00105         ->quoteIdentifier($table.$identifier);
00106 
00107       // Wert bestimmen
00108       $value = (isset($values[$identifier]) ? $values[$identifier] :
00109         $this->__get($identifier));
00110 
00111       // Where-Klausel hinzufügen
00112       $select->$whereMethod($identifierQuoted.' = ?', $value);
00113     }
00114   }
00115 
00121   protected function _checkValidity()
00122   {
00123     // Falls die Elternklasse das Objekt als gültig markiert hat und entweder
00124     // Identifizierer vorhanden oder Objekt noch synchron
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     // Identifizierer durchgehen
00146     foreach ($this->_identifiers as $index => $identifiers)
00147     {
00148       $identifiers = (array) $identifiers;
00149 
00150       foreach ($identifiers as $identifier)
00151       {
00152         // Falls einer der Identifizierer nicht existiert, direkt in den
00153         // nächsten Durchlauf der darüber liegenden Schleife springen
00154         if (!$this->__isset($identifier))
00155         {
00156           break 2;
00157         }
00158       }
00159 
00160       $identifiersValid[] = $identifiers;
00161 
00162       // Falls nach erstem Fund abgebrochen werden soll
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     // Falls Zeile gefunden wurde
00186     if ($row)
00187     {
00188       $synchronous = true;
00189 
00190       // Falls kein Array übergeben wurde, umwandeln
00191       if (!is_array($row))
00192       {
00193         $row = $row->toArray();
00194       }
00195 
00196       // Daten setzen
00197       $this->setData($row);
00198     }
00199 
00200     // Falls Synchronität gesetzt werden soll
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     // Falls Objekt synchron
00220     if (true === $this->_synchronous)
00221     {
00222       $this->setSynchronous(false);
00223     }
00224   }
00225 
00231   public function delete()
00232   {
00233     // Ggf. Exception werfen
00234     $this->isReadOnly(true);
00235     $this->hasIdentifier(true);
00236 
00237     // Synchronität zurücksetzen
00238     $this->_synchronous = null;
00239 
00240     return $this;
00241   }
00242 
00249   public function exists()
00250   {
00251     // Falls Objekt noch synchron
00252     if ($this->isSynchronous())
00253     {
00254       return true;
00255     }
00256     // Falls keine Identifizierer vorhanden
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     // Falls Daten erneut geladen werden sollen
00276     if ($reload)
00277     {
00278       // Daten erneut aus Datenbank laden
00279       $this->refresh();
00280     }
00281 
00282     return parent::getData($properties, $default);
00283   }
00284 
00285 
00292   public function getTable($name = null)
00293   {
00294     // Falls kein Tabellenname übergeben wurde
00295     if (null == $name)
00296     {
00297       // Falls Tabelle noch nicht instanziiert wurde
00298       if (is_string($this->_table))
00299       {
00300         $this->_table = Dsao_Registry::getTable($this->_table);
00301       }
00302 
00303       return $this->_table;
00304     }
00305     // Ansonsten Tabelle mit übergebenem Namen zurück geben
00306     else
00307     {
00308       return Dsao_Registry::getTable($name);
00309     }
00310   }
00311 
00319   public function hasIdentifier($throw = false)
00320   {
00321     // Falls gültige Identifizierer vorhanden oder Objekt synchron
00322     if ($this->isSynchronous() || $this->_getValidIdentifiers())
00323     {
00324       return true;
00325     }
00326     // Ansonsten ggf. Exception werfen
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     // Falls keine Änderungen vorgenommen wurden
00346     if (true === $this->isSynchronous())
00347     {
00348       return $this;
00349     }
00350     // Falls Objekt bereits in Datenbank
00351     else if (false === $this->isSynchronous())
00352     {
00353       throw new Dsao_Exception
00354         ('dsao_db_table_datahandler_abstract_insert_synchronous');
00355     }
00356 
00357     // Falls Objekt nicht gültig, abbrechen
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     // Ggf. Exception werfen
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     // Wenn Objekt synchron ist, muss es auch gültig sein
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     // Falls noch nie synchron gewesen, also nicht mit Datenbank verbunden
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 }

Erzeugt am Fri Sep 18 19:04:11 2009 für DSA online - Morgendaemmerung von  doxygen 1.5.7.1