Changeset 2392

Show
Ignore:
Timestamp:
03/31/2008 03:14:16 PM (8 months ago)
Author:
Shadowhand
Message:

Polishing up pdomo:

  • Added models/user_token.php
  • pdomo is now compatible with Auth and can be used instead of ORM
  • Added comments to helpers/pdomo.php
  • Updated comments in models/pdo.php
  • Added PDO_Model::delete()
  • Added in-class events to PDO_Model: on_construct, on_find, on_save, on_delete
Location:
trunk/modules/pdomo
Files:
4 added
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/pdomo/controllers/pdomo_demo.php

    r2383 r2392  
    77                pdomo::registry('default', new PDO('mysql:host=localhost;dbname=kohana', 'root', 'r00tdb')); 
    88 
    9                 $model = pdomo::factory('user')->find('username', 'woody.gilk'); 
     9                $m = pdomo::factory('user_token')->find(1); 
    1010 
    11                 $model->email = 'none'; 
    12  
    13                 echo Kohana::debug($model->save()); 
    14  
    15                 echo Kohana::debug($model); 
     11                echo Kohana::debug($m); 
    1612                echo Kohana::debug('{execution_time}'); 
    1713        } 
  • trunk/modules/pdomo/helpers/pdomo.php

    r2383 r2392  
    11<?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 */ 
    313class pdomo_Core { 
    414 
     15        // Default database instance 
    516        protected static $instance; 
    617 
     18        // Database registry 
    719        protected static $registry = array(); 
    820 
     21        /** 
     22         * Returns the default database instance. 
     23         * 
     24         * @return  object 
     25         */ 
    926        public static function instance() 
    1027        { 
     
    1229        } 
    1330 
     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         */ 
    1438        public static function registry($name, $db = NULL) 
    1539        { 
     
    2953        } 
    3054 
     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         */ 
    3165        public static function factory($name, $db = NULL) 
    3266        { 
     
    4377                else 
    4478                { 
    45                         throw new Kohana_User_Exception('pdomo Error', 'No database instance found.'); 
     79                        throw new Kohana_Exception('pdo.no_database_instance'); 
    4680                } 
    4781 
  • trunk/modules/pdomo/models/pdo.php

    r2385 r2392  
    55 * $Id$ 
    66 * 
    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 
    911 */ 
    1012abstract class PDO_Model { 
     
    3941         * @return  void 
    4042         */ 
    41         public function __construct(PDO $db) 
     43        public function __construct($db = NULL) 
    4244        { 
    4345                if ($this->table === NULL) 
     
    5052                        throw new Kohana_Exception('pdo.invalid_types', get_class($this)); 
    5153 
     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 
    5261                // Set the database instance 
    5362                $this->db = $db; 
     
    5564                // Empty the data 
    5665                $this->__empty_data(); 
     66 
     67                // Call the on_construct event 
     68                $this->__on_construct(); 
    5769        } 
    5870 
     
    6678        { 
    6779                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)); 
    6981 
    7082                // Return the key value 
     
    173185         * checking the return value of $this->loaded(). 
    174186         * 
    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) 
    179192        { 
    180193                // Empty the data 
    181194                $this->__empty_data(); 
    182195 
    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) 
    184200                { 
    185201                        // Load the data of the object 
    186                         $this->data = $result->fetch(PDO::FETCH_ASSOC); 
     202                        $this->data = $query->fetch(PDO::FETCH_ASSOC); 
    187203 
    188204                        // No data has been changed 
     
    198214 
    199215        /** 
    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        } 
    205267 
    206268        /** 
     
    284346 
    285347                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)); 
    287349 
    288350                // Quote the value 
     
    290352 
    291353                // 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(); 
    293358 
    294359                return $this; 
     
    347412                } 
    348413 
    349                 if ($result = $this->db->query($sql)) 
     414                if ($count = $this->db->exec($sql)) 
    350415                { 
    351416                        if ($insert === TRUE AND $this->auto_increment === TRUE) 
     
    361426                        $this->saved = $this->loaded = TRUE; 
    362427 
     428                        // Execute the on_save event 
     429                        $this->__on_save(); 
     430 
    363431                        // Success! 
    364432                        return TRUE; 
     
    369437        } 
    370438 
     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 
    371475} // End PDO_Model 
  • trunk/modules/pdomo/models/user.php

    r2383 r2392  
    11<?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 */ 
    312class User_Model extends PDO_Model { 
    413 
     
    1221                'logins' => 'integer', 
    1322        ); 
     23 
     24        /* PDO_Model Methods */ 
    1425 
    1526        protected function __validate()