Show
Ignore:
Timestamp:
01/01/06 21:37:32 (6 years ago)
Author:
john
Message:

added a bunch of stuff for scaffolding

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/active_record.php

    r94 r106  
    22# $Id$ 
    33# 
    4 # Copyright (c) 2005 John Peterson  
     4# Copyright (c) 2005 John Peterson 
    55# 
    66# Permission is hereby granted, free of charge, to any person obtaining 
     
    3030    static private $db = null;              # Reference to Pear db object 
    3131    static protected $inflector = null;     # object to do class inflection 
    32     public $table_info = null;              # info about each column in the table 
     32    public $content_columns = null;         # info about each column in the table 
    3333    public $table_name = null; 
    3434    public $fetch_mode = DB_FETCHMODE_ASSOC; 
    3535 
    3636    # Table associations 
    37     protected $has_many = array(); 
    38     protected $has_one = array(); 
    39     protected $has_and_belongs_to_many = array(); 
    40     protected $belongs_to = array(); 
    41     protected $habtm_attributes = array(); 
     37    protected $has_many = null; 
     38    protected $has_one = null; 
     39    protected $has_and_belongs_to_many = null; 
     40    protected $belongs_to = null; 
     41    protected $habtm_attributes = null; 
    4242 
    4343    protected $new_record = true;  # whether or not to create a new record or just update 
     
    4949    public $rows_per_page_default = 20;  # Pagination rows to display per page 
    5050    public $display = 10; # Pagination how many numbers in the list << < 1 2 3 4 > >> 
    51     public $errors = array();  
     51    public $errors = array(); 
    5252    public $auto_timestamps = true; # whether or not to auto update created_at/on and updated_at/on fields 
    5353    public $auto_save_habtm = true; # auto insert / update $has_and_belongs_to_many tables 
    5454 
    5555    # Transactions (only use if your db supports it) 
    56     private $begin_executed = false; # this is for transactions only to let query() know that a 'BEGIN' has been executed 
    57     public $use_transactions = false; # this will issue a rollback command if any sql fails 
     56    static private $begin_executed = false; # this is for transactions only to let query() know that a 'BEGIN' has been executed 
     57    static public $use_transactions = false; # this will issue a rollback command if any sql fails 
    5858 
    5959    # Constructor sets up need parameters for AR to function properly 
    6060    function __construct($attributes = null) { 
    61  
    62         # Define static members 
    63         if (self::$inflector == null) { 
    64             self::$inflector = new Inflector(); 
    65         } 
    66  
    6761        # Open the database connection 
    6862        $this->establish_connection(); 
    6963 
    7064        # Set $table_name 
    71         if($this->table_name == null) {  
     65        if($this->table_name == null) { 
    7266            $this->set_table_name_using_class_name(); 
    7367        } 
     
    7569        # Set column info 
    7670        if($this->table_name) { 
    77             $this->set_table_info($this->table_name); 
     71            $this->set_content_columns($this->table_name); 
    7872        } 
    7973 
     
    130124        //echo "setting: $key = $value<br>"; 
    131125        if($key == "table_name") { 
    132             $this->set_table_info($value); 
     126            $this->set_content_columns($value); 
    133127        } 
    134128        $this->$key = $value; 
     
    223217    # Returns: An array of ActiveRecord objects. (e.g. Movie objects) 
    224218    function find_all_habtm($other_table_name, $parameters = null) { 
    225         $other_class_name = self::$inflector->classify($other_table_name); 
     219        $other_class_name = Inflector::classify($other_table_name); 
    226220        # Instantiate an object to access find_all 
    227221        $results = new $other_class_name(); 
     
    229223        # Prepare the join table name primary keys (fields) to do the join on 
    230224        $join_table = $this->get_join_table_name($this->table_name, $other_table_name); 
    231         $this_foreign_key = self::$inflector->singularize($this->table_name)."_id"; 
    232         $other_foreign_key = self::$inflector->singularize($other_table_name)."_id"; 
     225        $this_foreign_key = Inflector::singularize($this->table_name)."_id"; 
     226        $other_foreign_key = Inflector::singularize($other_table_name)."_id"; 
    233227        # Set up the SQL segments 
    234228        $conditions = "`{$join_table}`.`{$this_foreign_key}`={$this->id}"; 
     
    281275            $foreign_key = $parameters['foreign_key']; 
    282276        else 
    283             $foreign_key = self::$inflector->singularize($this->table_name)."_id"; 
    284  
    285         $other_class_name = self::$inflector->classify($other_table_name); 
     277            $foreign_key = Inflector::singularize($this->table_name)."_id"; 
     278 
     279        $other_class_name = Inflector::classify($other_table_name); 
    286280        $conditions = "`{$foreign_key}`=$this->id"; 
    287281 
     
    332326        # customers has_many contacts, then we'll need a Contact 
    333327        # object, and the customer_id field name. 
    334         $other_class_name = self::$inflector->camelize($other_object_name); 
     328        $other_class_name = Inflector::camelize($other_object_name); 
    335329        if(@array_key_exists("foreign_key", $parameters)) 
    336330            $foreign_key = $parameters['foreign_key']; 
    337331        else 
    338             $foreign_key = self::$inflector->singularize($this->table_name)."_id"; 
     332            $foreign_key = Inflector::singularize($this->table_name)."_id"; 
    339333 
    340334        $conditions = "`$foreign_key`='{$this->id}'"; 
     
    361355        # customers has_many contacts, then we'll need a Contact 
    362356        # object, and the customer_id field name. 
    363         $other_class_name = self::$inflector->camelize($other_object_name); 
     357        $other_class_name = Inflector::camelize($other_object_name); 
    364358        if(@array_key_exists("foreign_key", $parameters)) 
    365359            $foreign_key = $parameters['foreign_key']; 
     
    406400    } 
    407401 
     402    # Return if this is a new record or not. 
     403    function is_new_record() { 
     404        return $this->new_record; 
     405    } 
     406 
     407    # get the attributes for a specific column. 
     408    function column_for_attribute($attribute) { 
     409        if(is_array($this->content_columns)) { 
     410            foreach($this->content_columns as $column) { 
     411                if($column['name'] == $attribute) { 
     412                    return $column; 
     413                } 
     414            } 
     415        } 
     416        return null; 
     417    } 
     418 
    408419    # Returns PEAR result set of one record with only the passed in column in the result set. 
    409420    function send($column) { 
     
    440451        $rs = self::$db->query($sql); 
    441452        if ($this->is_error($rs)) { 
    442             if($this->use_transactions && $this->begin_executed) { 
     453            if(self::$use_transactions && self::$begin_executed) { 
    443454                $this->rollback(); 
    444455            } 
     
    452463    function find_all($conditions = null, $orderings = null, $limit = null, $joins = null) { 
    453464        if (is_array($limit)) { 
    454             list($this->page, $this->rows_per_page) = $limit; 
    455             if($this->page <= 0) $this->page = 1; 
     465            list(self::$page, self::$rows_per_page) = $limit; 
     466            if(self::$$page <= 0) self::$page = 1; 
    456467            # Default for rows_per_page: 
    457             if ($this->rows_per_page == null) $this->rows_per_page = $this->rows_per_page_default; 
     468            if (self::$rows_per_page == null) self::$rows_per_page = self::$rows_per_page_default; 
    458469            # Set the LIMIT string segment for the SQL in the find_all 
    459             $this->offset = ($this->page - 1) * $this->rows_per_page; 
     470            self::$offset = (self::$page - 1) * self::$rows_per_page; 
    460471            # mysql 3.23 doesn't support OFFSET 
    461472            //$limit = "$rows_per_page OFFSET $offset"; 
    462             $limit = "$this->offset, $this->rows_per_page"; 
     473            $limit = self::$offset.", ".self::$rows_per_page; 
    463474            $set_pages = true; 
    464475        } 
     
    467478            $sql = $conditions; 
    468479        } else { 
    469             $sql  = "SELECT * FROM `$this->table_name` "; 
     480            $sql  = "SELECT * FROM `".$this->table_name."` "; 
    470481            if(!is_null($joins)) { 
    471482                if(substr($joins,0,4) != "LEFT") $sql .= ","; 
     
    477488                if($set_pages) { 
    478489                    //echo "ActiveRecord::find_all() - sql: $sql\n<br>"; 
    479                     if($this->is_error($rs = $this->query($sql))) { 
    480                         $this->raise($rs->getMessage()); 
     490                    if(self::$is_error($rs = self::query($sql))) { 
     491                        self::raise($rs->getMessage()); 
    481492                    } else { 
    482493                        # Set number of total pages in result set without the LIMIT 
    483494                        if($count = $rs->numRows()) 
    484                             $this->pages = (($count % $this->rows_per_page) == 0) ? $count / $this->rows_per_page : floor($count / $this->rows_per_page) + 1; 
     495                            self::$pages = (($count % self::$rows_per_page) == 0) ? $count / self::$rows_per_page : floor($count / self::$rows_per_page) + 1; 
    485496                    } 
    486497                } 
     
    490501 
    491502        //echo "ActiveRecord::find_all() - sql: $sql\n<br>"; 
    492         if($this->is_error($rs = $this->query($sql))) { 
    493             $this->raise($rs->getMessage()); 
     503        if(self::is_error($rs = self::query($sql))) { 
     504            self::raise($rs->getMessage()); 
    494505        } 
    495506 
    496507        $objects = array(); 
    497508        while($row = $rs->fetchRow()) { 
    498             $class = get_class($this); 
     509            #$class = get_class($this); 
     510            $class = Inflector::classify($this->table_name); 
    499511            $object = new $class(); 
    500512            $object->new_record = false; 
     
    525537 
    526538        if(is_array($id)) { 
    527             return $this->find_all($conditions, $orderings, $limit, $joins); 
     539            return /*$this->*/find_all($conditions, $orderings, $limit, $joins); 
    528540        } else { 
    529541            return $this->find_first($conditions, $orderings, $limit, $joins); 
     
    777789                foreach($this->habtm_attributes as $other_table_name => $other_foreign_values) { 
    778790                    $table_name = $this->get_join_table_name($this->table_name,$other_table_name); 
    779                     $other_foreign_key = self::$inflector->singularize($other_table_name)."_id"; 
    780                     $this_foreign_key = self::$inflector->singularize($this->table_name)."_id"; 
     791                    $other_foreign_key = Inflector::singularize($other_table_name)."_id"; 
     792                    $this_foreign_key = Inflector::singularize($this->table_name)."_id"; 
    781793                    foreach($other_foreign_values as $other_foreign_value) { 
    782794                        unset($attributes); 
     
    804816            foreach($this->habtm_attributes as $other_table_name => $values) { 
    805817                $table_name = $this->get_join_table_name($this->table_name,$other_table_name); 
    806                 $this_foreign_key = self::$inflector->singularize($this->table_name)."_id"; 
     818                $this_foreign_key = Inflector::singularize($this->table_name)."_id"; 
    807819                $sql = "DELETE FROM $table_name WHERE $this_foreign_key = $this_foreign_value"; 
    808820                //echo "delete_habtm_records: SQL: $sql<br>"; 
     
    845857    } 
    846858 
    847     # If $this->set_table_info() was previously called, which will mean that  
     859    # If $this->set_content_columns() was previously called, which will mean that 
    848860    # $table_info will be an array of containing the column info about the database 
    849861    # table this model is representing.  This will return an array where the keys 
     
    851863    function get_attributes() { 
    852864        $attributes = array(); 
    853         if(is_array($this->table_info)) { 
    854             foreach($this->table_info as $info) { 
     865        if(is_array($this->content_columns)) { 
     866            foreach($this->content_columns as $column) { 
    855867                //echo "attribute: $info[name] -> {$this->$info[name]}<br>"; 
    856                 $attributes[$info['name']] = $this->$info['name']; 
     868                $attributes[$column['name']] = $this->$column['name']; 
    857869            } 
    858870        } 
     
    927939    # used in all queries throughout ActiveRecord 
    928940    function set_table_name_using_class_name() { 
    929         if(!isset($this->table_name)) { 
    930             $this->table_name = self::$inflector->tableize(get_class($this)); 
     941        if(!$this->table_name) { 
     942            $this->table_name = Inflector::tableize(get_class($this)); 
    931943        } 
    932944    } 
    933945 
    934946    # Populates the model object with information about the table it represents 
    935     function set_table_info($table_name) { 
    936         $this->table_info = self::$db->tableInfo($table_name); 
    937         if(is_array($this->table_info)) { 
     947    function set_content_columns($table_name) { 
     948        $this->content_columns = self::$db->tableInfo($table_name); 
     949        if(is_array($this->content_columns)) { 
    938950            $i = 0; 
    939             foreach($this->table_info as $info) { 
    940                 $this->table_info[$i++]['human_name'] = self::$inflector->humanize($info['name']); 
     951            foreach($this->content_columns as $column) { 
     952                $this->content_columns[$i++]['human_name'] = Inflector::humanize($column['name']); 
    941953            } 
    942954        } 
     
    9891001        $error_message  = "Model Class: ".get_class($this)."<br>"; 
    9901002        $error_message .= "Error Message: ".$message; 
    991         throw new ActiveRecordError($error_message, "ActiveRecord Error", "500");         
     1003        throw new ActiveRecordError($error_message, "ActiveRecord Error", "500"); 
    9921004    } 
    9931005