Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
model.class.php
Go to the documentation of this file.
00001 <?php
00041 class model {
00042 
00048         protected $_p = array();
00049 
00055         protected $_chgP = array();
00056 
00057 
00061         protected $_tbl_data = '';
00062 
00066         protected $_registry;
00067 
00071         protected $_id_name = 'id';
00072 
00080         function __construct($id, $table=null) {
00081                 
00082                 $this->_registry = registry::instance();
00083 
00084                 if(!is_null($table)) $this->_tbl_data = $table;
00085 
00086                 $this->_p = $this->initDbProp($id);
00087 
00088         }
00089 
00096         public function setIdName($id_name) {
00097 
00098                 $this->_id_name = $id_name;
00099 
00100         }
00101 
00108         public function setTable($table) {
00109                 
00110                 $this->_tbl_data = $table;
00111 
00112         }
00113 
00123         protected function initDbProp($id) {
00124 
00125                 $qr = $this->_registry->db->autoSelect(array("*"), array($this->_tbl_data), "id='$id'", null);
00126                 if(count($qr)) {
00127                     $structure = $this->_registry->db->getTableStructure($this->_tbl_data);
00128                     $res = array();
00129                     foreach($qr[0] as $fname=>$fvalue) {
00130                         if($structure['fields'][$fname]['type']=='int') setType($fvalue, 'int');
00131                         elseif($structure['fields'][$fname]['type']=='float' || $structure['fields'][$fname]['type']=='double' || $structure['fields'][$fname]['type']=='decimal') setType($fvalue, 'float');
00132                         else setType($fvalue, 'string');
00133                         $res[$fname] = $fvalue;
00134                     }
00135                     return $res;
00136                 }
00137                 else return $this->initNullProp();
00138 
00139         }
00140 
00147         protected function initNullProp() {
00148 
00149                 $res = array();
00150                 $fields = $this->_registry->db->getFieldsName($this->_tbl_data);
00151                 foreach($fields as $f) $res[$f] = null;
00152 
00153                 return $res;
00154 
00155         }
00156 
00166         public function __get($pName) {
00167 
00168                 if(!array_key_exists($pName, $this->_p)) return null;
00169                 if(method_exists($this, 'get'.$pName)) return $this->{'get'.$pName}();
00170                 else return $this->_p[$pName];
00171         }
00172         
00183         public function __set($pName, $value) {
00184 
00185                 if(!array_key_exists($pName, $this->_p)) return false;
00186                 if(method_exists($this, 'set'.$pName)) return $this->{'set'.$pName}($value);
00187                 else {
00188                         if($this->_p[$pName]!=$value && !in_array($pName, $this->_chgP)) $this->_chgP[] = $pName;
00189                         $this->_p[$pName] = $value;
00190                         return true;
00191                 }
00192         }
00193 
00203         public function saveData($insert=false) {
00204         
00205                 $data = array();
00206                 foreach($this->_chgP as $f) $data[$f] = $this->_p[$f];
00207 
00208                 if(!$this->_p[$this->_id_name] || $insert) {
00209                         $res = $this->_registry->db->insert($this->_tbl_data, $data);
00210                         if($res) $this->_p[$this->_id_name] = $res;
00211                 }
00212                 else 
00213                         $res = $this->_registry->db->update($this->_tbl_data, $data, $this->_id_name."='".$this->_p[$this->_id_name]."'");
00214 
00215                 return $res;
00216         }
00217 
00224         public function deleteData() {
00225         
00226                 return $this->_registry->db->delete($this->_tbl_data, $this->_id_name."=".$this->_p[$this->_id_name]);
00227         }
00228 
00229 }
00230 
00231 ?>