Abstract.php

Go to the documentation of this file.
00001 <?php
00002 
00033 abstract class Robo47_Validate_Doctrine_Abstract extends Zend_Validate_Abstract
00034 {
00038     const ERROR_NO_RECORD_FOUND = 'noRecordFound';
00039     const ERROR_RECORD_FOUND = 'recordFound';
00040 
00044     protected $_messageTemplates = array(
00045         self::ERROR_NO_RECORD_FOUND => 'No record matching %value% was found',
00046         self::ERROR_RECORD_FOUND => 'A record matching %value% was found',
00047     );
00051     protected $_table = '';
00055     protected $_field = '';
00059     protected $_exclude = null;
00060 
00067     public function __construct($table, $field, $exclude = null)
00068     {
00069         $this->setTable($table);
00070         $this->setField($field);
00071         $this->setExclude($exclude);
00072     }
00073 
00079     public function getExclude()
00080     {
00081         return $this->_exclude;
00082     }
00083 
00090     public function setExclude($exclude)
00091     {
00092         $this->_exclude = $exclude;
00093         return $this;
00094     }
00095 
00101     public function getField()
00102     {
00103         return $this->_field;
00104     }
00105 
00112     public function setField($field)
00113     {
00114         $this->_field = (string) $field;
00115         return $this;
00116     }
00117 
00124     public function setTable($table)
00125     {
00126         if (is_string($table)) {
00127             $table = Doctrine_Core::getTable($table);
00128         }
00129         if (!$table instanceof Doctrine_Table) {
00130             $message = 'table not instance of Doctrine_Table.';
00131             throw new Robo47_Log_Writer_Exception($message);
00132         }
00133         $this->_table = $table;
00134         return $this;
00135     }
00136 
00142     public function getTable()
00143     {
00144         return $this->_table;
00145     }
00146 
00153     protected function _query($value)
00154     {
00155         $query = $this->getTable()->createQuery();
00156 
00157         $exclude = $this->getExclude();
00158         if (is_string($exclude) && !empty($exclude)) {
00159             $query->andWhere($exclude);
00160         } else if (is_array($exclude) && !empty($exclude)) {
00161             $query->andWhere($exclude['field'] . ' != ?', $exclude['value']);
00162         }
00163 
00164         return $query->andWhere($this->getField() . ' = ?', $value)
00165             ->limit(1)
00166             ->execute();
00167     }
00168 }