Axiom (v1.2.0)

A lightweight PHP framework
H:/Workspace/php-axiom/www/php-axiom/libraries/axiom/axBaseModel.class.php
Go to the documentation of this file.
00001 <?php
00020 abstract class axBaseModel implements axModel {
00021 
00026     protected $_pdo;
00027     
00032     protected $_idKey = "id";
00033 
00038     protected $_data = array();
00039 
00044     protected $_statements = array();
00045 
00062     abstract protected function _init ($statement);
00063 
00074     public function __construct (PDO $pdo, $id = null) {
00075         if (!$pdo)
00076             throw new InvalidArgumentException('First parameters is expected to be a valid PDO instance');
00077         
00078         $this->_pdo = $pdo;
00079             
00080         if ($id !== null && $id !== false && !$this->retrieve($id))
00081             throw new RuntimeException("Cannot instanciate model");
00082     }
00083 
00088     public function __sleep () {
00089         return array('_idKey', '_data');
00090     }
00091 
00100     public function __get ($key) {
00101         return isset($this->_data[$key]) ? $this->_data[$key] : null;
00102     }
00103 
00114     public function __set ($key, $value) {
00115         $this->_data[$key] = $value;
00116     }
00117 
00126     public function __isset ($key) {
00127         return isset($this->_data[$key]);
00128     }
00129 
00137     public function getData () {
00138         return $this->_data;
00139     }
00140 
00151     public function create (array $data) {
00152         if (!$this->_init("create"))
00153             throw new RuntimeException("Cannot initialize " . __METHOD__, 2011);
00154          
00155         if ($this->_statements['create']->execute($data)) {
00156             $id = $this->_pdo->lastInsertId();
00157             return $this->retrieve($id);
00158         }
00159         return false;
00160     }
00161     
00171     public function retrieve ($id) {
00172         if (!$this->_init("retrieve"))
00173             throw new RuntimeException("Cannot initialize " . __METHOD__, 2010);
00174          
00175         if ($this->_statements['retrieve']->execute(array(":{$this->_idKey}" => $id))) {
00176             if ($this->_statements['retrieve']->rowCount()) {
00177                 $this->_data = $this->_statements['retrieve']->fetch(PDO::FETCH_ASSOC);
00178                 return $this;
00179             }
00180         }
00181         return false;
00182     }
00183 
00194     public function update (array $data = array()) {
00195         if (!$this->_init("update"))
00196             throw new RuntimeException("Cannot initialize " . __METHOD__, 2012);
00197          
00198         if (!empty($this->_data)) {
00199             $inputs = array_merge($this->_data, array_intersect_key($data, $this->_data));
00200             return $this->_statements['update']->execute($inputs) ? $this : false;
00201         }
00202         return false;
00203     }
00204 
00213     public function delete () {
00214         if (!$this->_init("delete"))
00215             throw new RuntimeException("Cannot initialize " . __METHOD__, 2013);
00216          
00217         if (!empty($this->_data))
00218             return $this->_statements['delete']->execute(array(":{$this->_idKey}" => $this->_data[$this->_idKey]));
00219         return false;
00220     }
00221 }
 All Data Structures Files Functions Variables