Changeset 2392
- Timestamp:
- 03/31/2008 03:14:16 PM (8 months ago)
- Location:
- trunk/modules/pdomo
- Files:
-
- 4 added
- 4 modified
-
controllers/pdomo_demo.php (modified) (1 diff)
-
helpers/pdomo.php (modified) (4 diffs)
-
i18n (added)
-
i18n/en_US (added)
-
i18n/en_US/pdo.php (added)
-
models/pdo.php (modified) (12 diffs)
-
models/user.php (modified) (2 diffs)
-
models/user_token.php (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/pdomo/controllers/pdomo_demo.php
r2383 r2392 7 7 pdomo::registry('default', new PDO('mysql:host=localhost;dbname=kohana', 'root', 'r00tdb')); 8 8 9 $m odel = pdomo::factory('user')->find('username', 'woody.gilk');9 $m = pdomo::factory('user_token')->find(1); 10 10 11 $model->email = 'none'; 12 13 echo Kohana::debug($model->save()); 14 15 echo Kohana::debug($model); 11 echo Kohana::debug($m); 16 12 echo Kohana::debug('{execution_time}'); 17 13 } -
trunk/modules/pdomo/helpers/pdomo.php
r2383 r2392 1 1 <?php defined('SYSPATH') or die('No direct script access.'); 2 2 /** 3 * Kohana PDO Model helper. This manages database instances and acts as a 4 * factory for PDO models. 5 * 6 * $Id$ 7 * 8 * @package pdomo 9 * @author Woody Gilk 10 * @copyright (c) 2007 Kohana Team 11 * @license http://kohanaphp.com/license.html 12 */ 3 13 class pdomo_Core { 4 14 15 // Default database instance 5 16 protected static $instance; 6 17 18 // Database registry 7 19 protected static $registry = array(); 8 20 21 /** 22 * Returns the default database instance. 23 * 24 * @return object 25 */ 9 26 public static function instance() 10 27 { … … 12 29 } 13 30 31 /** 32 * Gets and sets database instances from the registry. 33 * 34 * @param string database name 35 * @param object PDO instance 36 * @return object 37 */ 14 38 public static function registry($name, $db = NULL) 15 39 { … … 29 53 } 30 54 55 /** 56 * Acts as a factory for PDO models. By default, all models will be created 57 * with the default database instance. To use a different database instance, 58 * pass the instance name as the second parameter. 59 * 60 * @throws Kohana_Exception 61 * @param string model name 62 * @param string database name 63 * @return object 64 */ 31 65 public static function factory($name, $db = NULL) 32 66 { … … 43 77 else 44 78 { 45 throw new Kohana_ User_Exception('pdomo Error', 'No database instance found.');79 throw new Kohana_Exception('pdo.no_database_instance'); 46 80 } 47 81 -
trunk/modules/pdomo/models/pdo.php
r2385 r2392 5 5 * $Id$ 6 6 * 7 * @package pdomo 8 * @author Woody Gilk 7 * @package pdomo 8 * @author Woody Gilk 9 * @copyright (c) 2007 Kohana Team 10 * @license http://kohanaphp.com/license.html 9 11 */ 10 12 abstract class PDO_Model { … … 39 41 * @return void 40 42 */ 41 public function __construct( PDO $db)43 public function __construct($db = NULL) 42 44 { 43 45 if ($this->table === NULL) … … 50 52 throw new Kohana_Exception('pdo.invalid_types', get_class($this)); 51 53 54 // Get a database instance 55 ($db === NULL) and $db = pdomo::instance(); 56 57 // Makes sure the database instance is valid 58 if ( ! ($db instanceof PDO)) 59 throw new Kohana_Exception('pdo.invalid_database', get_class($this)); 60 52 61 // Set the database instance 53 62 $this->db = $db; … … 55 64 // Empty the data 56 65 $this->__empty_data(); 66 67 // Call the on_construct event 68 $this->__on_construct(); 57 69 } 58 70 … … 66 78 { 67 79 if ( ! isset($this->data[$key])) 68 throw new Kohana_Exception('pdo.invalid_get', get_class($this), $key);80 throw new Kohana_Exception('pdo.invalid_get', $key, get_class($this)); 69 81 70 82 // Return the key value … … 173 185 * checking the return value of $this->loaded(). 174 186 * 175 * @param string SQL query 176 * @return void 177 */ 178 protected function __query($sql) 187 * @param object PDOStatement to be executed 188 * @param array optional array of arguments to be passed to execute() 189 * @return void 190 */ 191 protected function __query(PDOStatement $query, $params = NULL) 179 192 { 180 193 // Empty the data 181 194 $this->__empty_data(); 182 195 183 if ($result = $this->db->query($sql)) 196 // Execute the query 197 $status = empty($params) ? $query->execute() : $query->execute($params); 198 199 if ($status > 0 AND $query->rowCount() > 0) 184 200 { 185 201 // Load the data of the object 186 $this->data = $ result->fetch(PDO::FETCH_ASSOC);202 $this->data = $query->fetch(PDO::FETCH_ASSOC); 187 203 188 204 // No data has been changed … … 198 214 199 215 /** 200 * Validation check. This must be defined in all models. 201 * 202 * @return boolean 203 */ 204 abstract protected function __validate(); 216 * Called as the last step of __construct(), before a return. 217 * 218 * @return void 219 */ 220 protected function __on_construct() 221 { 222 // No default action 223 } 224 225 /** 226 * Called as the last step of find(), before a return. 227 * 228 * @return void 229 */ 230 protected function __on_find() 231 { 232 // No default action 233 } 234 235 /** 236 * Called as the last step of save(), before a return. 237 * 238 * @return void 239 */ 240 protected function __on_save() 241 { 242 // No default action 243 } 244 245 /** 246 * Called as the last step of delete(), before a return. 247 * 248 * @return void 249 */ 250 protected function __on_delete() 251 { 252 // No default action 253 } 254 255 /** 256 * Validation method, called by save() to determine if the model can be saved. 257 * If validation is successful, TRUE should be returned. If validation fails, 258 * an array(field => error) should be returned. 259 * 260 * @return boolean (success) TRUE 261 * @return array (failure) array of errors 262 */ 263 protected function __validate() 264 { 265 return TRUE; 266 } 205 267 206 268 /** … … 284 346 285 347 if ( ! preg_match('/^=|[!<>]=?|(?:NOT\s+)?(LIKE|REGEXP?|IN)$/i', $op)) 286 throw new Kohana_Exception('pdo.invalid_operation', get_class($this), $op);348 throw new Kohana_Exception('pdo.invalid_operation', $op, get_class($this)); 287 349 288 350 // Quote the value … … 290 352 291 353 // Find a single row matching the criteria 292 $this->__query('SELECT '.$this->table.'.* FROM '.$this->table.' WHERE '.$key.' '.$op.' '.$value.' LIMIT 1 OFFSET 0'); 354 $this->__query($this->db->prepare('SELECT '.$this->table.'.* FROM '.$this->table.' WHERE '.$key.' '.$op.' '.$value.' LIMIT 1 OFFSET 0')); 355 356 // Execute the on_find event 357 $this->__on_find(); 293 358 294 359 return $this; … … 347 412 } 348 413 349 if ($ result = $this->db->query($sql))414 if ($count = $this->db->exec($sql)) 350 415 { 351 416 if ($insert === TRUE AND $this->auto_increment === TRUE) … … 361 426 $this->saved = $this->loaded = TRUE; 362 427 428 // Execute the on_save event 429 $this->__on_save(); 430 363 431 // Success! 364 432 return TRUE; … … 369 437 } 370 438 439 /** 440 * Deletes the current object from the database. 441 * 442 * @return boolean (failure) FALSE 443 * @return integer (success) number of rows deleted 444 */ 445 public function delete() 446 { 447 // Nothing is deleted by default 448 $status = FALSE; 449 450 if ( ! empty($this->data[$this->primary_key])) 451 { 452 // Primary key 453 $pk = $this->primary_key; 454 455 // SQL to delete this object 456 $sql = 'DELETE FROM '.$this->table.' WHERE '.$this->primary_key.' = '.$this->__quote_value($this->$pk); 457 458 if ($count = $this->db->exec($sql)) 459 { 460 // Clear the object 461 $this->__empty_data(); 462 463 // Return the count as the status 464 $status = $count; 465 } 466 467 // Call the on_delete event 468 $this->__on_delete(); 469 } 470 471 // Return the status 472 return $status; 473 } 474 371 475 } // End PDO_Model -
trunk/modules/pdomo/models/user.php
r2383 r2392 1 1 <?php defined('SYSPATH') or die('No direct script access.'); 2 2 /** 3 * PDO User_Model, a replacement for the default Auth User_Model (ORM). 4 * 5 * $Id$ 6 * 7 * @package pdomo 8 * @author Woody Gilk 9 * @copyright (c) 2007 Kohana Team 10 * @license http://kohanaphp.com/license.html 11 */ 3 12 class User_Model extends PDO_Model { 4 13 … … 12 21 'logins' => 'integer', 13 22 ); 23 24 /* PDO_Model Methods */ 14 25 15 26 protected function __validate()
