Changeset 1750

Show
Ignore:
Timestamp:
03/06/2007 02:40:51 PM (21 months ago)
Author:
xue
Message:

changed the way to specify active record table.

Location:
trunk
Files:
21 modified

Legend:

Unmodified
Added
Removed
  • trunk/UPGRADE

    r1749 r1750  
    1212Upgrading from v3.1a 
    1313--------------------- 
    14 - The signature of TActiveRecord::finder() is changed. All TActiveRecord-descendant 
    15   classes that override this method will be affected. Please use the 
    16   following code to override the method:         
     14- The signature of TActiveRecord::finder() is changed. This affects 
     15  all TActiveRecord-descendant classes that override this method. 
     16  Please use the following code to override the method:  
    1717        public static function finder($className=__CLASS__) 
    1818        { 
    1919                return parent::finder($className); 
    2020        } 
     21 
     22- The way to specify the table name for an active record class is changed. 
     23  Previously, it used the static class member '_tablename'.  
     24  Now it uses class constant as follows: 
     25    class UserRecord extends TActiveRecord 
     26    { 
     27        const TABLE='users_table'; 
     28    } 
    2129 
    2230Upgrading from v3.0.x 
  • trunk/demos/address-book/protected/pages/AddressRecord.php

    r1748 r1750  
    55class AddressRecord extends TActiveRecord 
    66{ 
    7         public static $_tablename='addresses'; 
     7        const TABLE='addresses'; 
    88 
    99        /** 
  • trunk/demos/chat/protected/App_Code/ChatBufferRecord.php

    r1729 r1750  
    33class ChatBufferRecord extends TActiveRecord 
    44{ 
     5        const TABLE='chat_buffer'; 
     6 
    57        public $id; 
    68        public $for_user; 
     
    810        public $message; 
    911        private $_created_on; 
    10  
    11         public static $_tablename='chat_buffer'; 
    1212 
    1313        public function getCreated_On() 
  • trunk/demos/chat/protected/App_Code/ChatUserRecord.php

    r1729 r1750  
    33class ChatUserRecord extends TActiveRecord 
    44{ 
     5        const TABLE='chat_users'; 
     6 
    57        public $username; 
    68        private $_last_activity; 
    7  
    8         public static $_tablename='chat_users'; 
    99 
    1010        public function getLast_Activity() 
  • trunk/demos/quickstart/protected/controls/Comments/CommentBlock.php

    r1729 r1750  
    1111class CommentRecord extends TActiveRecord 
    1212{ 
     13        const TABLE='qs_comments'; 
     14 
    1315        public $id; 
    1416        public $username; 
     
    1719        public $block_id; 
    1820        public $content; 
    19  
    20         public static $_tablename='qs_comments'; 
    2121 
    2222        public static function finder($className=__CLASS__) 
  • trunk/demos/quickstart/protected/pages/Database/ActiveRecord.page

    r1729 r1750  
    7474class UserRecord extends TActiveRecord 
    7575{ 
     76    const TABLE='users'; //table name  
     77 
    7678    public $username; //the column named "username" in the "users" table 
    7779    public $email; 
    78      
    79     public static $_tablename='users'; //table name  
    8080     
    8181    /** 
     
    9090</p> 
    9191<p id="690485" class="block-content">Each property of the <tt>UserRecord</tt> class must correspond to a 
    92     column with the same name in the "users" table. The static class variable 
    93     <tt>$_tablename</tt> (must be public) is optional when the class name is the same as 
    94     the table name in the database, otherwise <tt>$_tablename</tt> must 
     92    column with the same name in the "users" table. The class constant 
     93    <tt>TABLE</tt> is optional when the class name is the same as 
     94    the table name in the database, otherwise <tt>TABLE</tt> must 
    9595    specify the table name that corresponds to your Active Record class. 
    9696</p> 
    9797 
    9898<div class="note"><b class="note">Note:</b> 
    99 You may need to quote (specific to your database) the value of the <tt>$_tablename</tt>.  
    100 E.g. MySQL uses back-ticks, <tt>$_tablename = "`database1`.`table1`"</tt> 
     99You may need to quote (specific to your database) the value of the <tt>TABLE</tt>.  
     100E.g. MySQL uses back-ticks, <tt>TABLE = "`database1`.`table1`"</tt> 
    101101</div> 
    102102 
     
    121121 
    122122<div class="info"><b class="note">Info:</b> 
    123 <tt>TActiveRecord</tt> can also work with database views by specifying the value <tt>$_tablename</tt> 
     123<tt>TActiveRecord</tt> can also work with database views by specifying the constant <tt>TABLE</tt> 
    124124corresponding to the view name. However, objects returned 
    125125from views are read-only, calling the <tt>save()</tt> or <tt>delete()</tt> method 
  • trunk/demos/quickstart/protected/pages/Database/Samples/Scaffold/Home.php

    r1664 r1750  
    66class AddressRecord extends TActiveRecord 
    77{ 
     8        const TABLE='addresses'; 
     9 
    810        public $id; 
    911        public $username; 
    1012        public $phone; 
    11  
    12         public static $_tablename='addresses'; 
    1313 
    1414        //for demo, we use static db here 
  • trunk/demos/quickstart/protected/pages/Database/Scaffold.page

    r1664 r1750  
    4040class UserRecord extends TActiveRecord 
    4141{ 
     42        const TABLE='users'; 
     43         
    4244    public $username; 
    4345    public $email; 
    44      
    45     public static $_tablename='users'; 
    4646} 
    4747</com:TTextHighlighter> 
  • trunk/demos/quickstart/protected/pages/Database/SqlMap.page

    r1729 r1750  
    214214class UserRecord extends TActiveRecord 
    215215{ 
     216        const TABLE='users'; //table name  
     217 
    216218    public $username; //the column named "username" in the "users" table 
    217219    public $email; 
    218      
    219     private static $_tablename='users'; //table name  
    220      
     220         
    221221    /** 
    222222     * @return TActiveRecord active record finder instance 
  • trunk/demos/quickstart/protected/pages/Tutorial/AjaxChat.page

    r1729 r1750  
    169169class ChatUserRecord extends TActiveRecord 
    170170{ 
     171    const TABLE='chat_users'; 
     172     
    171173    public $username; 
    172174    public $last_activity; 
    173  
    174     public static $_tablename='chat_users'; 
    175175 
    176176    public static function finder($className=__CLASS__) 
     
    503503class ChatBufferRecord extends TActiveRecord 
    504504{ 
     505        const TABLE='chat_buffer'; 
     506 
    505507    public $id; 
    506508    public $for_user; 
     
    508510    public $message; 
    509511    private $_created_on; 
    510  
    511     public static $_tablename='chat_buffer'; 
    512512 
    513513    public function getCreated_On() 
  • trunk/demos/quickstart/protected/pages/Tutorial/fr/AjaxChat.page

    r1729 r1750  
    169169class ChatUserRecord extends TActiveRecord 
    170170{ 
     171        const TABLE='chat_users'; 
     172 
    171173    public $username; 
    172174    public $last_activity; 
    173  
    174     public static $_tablename='chat_users'; 
    175175 
    176176    public static function finder($className=__CLASS__) 
     
    503503class ChatBufferRecord extends TActiveRecord 
    504504{ 
     505        const TABLE='chat_buffer'; 
     506 
    505507    public $id; 
    506508    public $for_user; 
     
    508510    public $message; 
    509511    private $_created_on; 
    510  
    511     public static $_tablename='chat_buffer'; 
    512512 
    513513    public function getCreated_On() 
  • trunk/framework/Data/ActiveRecord/Exceptions/messages.txt

    r1729 r1750  
    1010ar_invalid_db_connection                                        = Missing or invalid default database connection for ActiveRecord class '{0}', default connection is set by the DbConnection property of TActiveRecordManager. 
    1111ar_mismatch_args_exception                                      = ActiveRecord finder method '{0}' expects {1} parameters but found only {2} parameters instead. 
    12 ar_invalid_tablename_property                           = ActiveRecord tablename property '{0}::${1}' must be static and not null. 
     12ar_invalid_tablename_property                           = Constant {0}::{1} must be a valid database table name. 
    1313ar_value_must_not_be_null                                       = Property '{0}::${2}' must not be null as defined by column '{2}' in table '{1}'. 
    1414ar_missing_pk_values                                            = Missing primary key values in forming IN(key1, key2, ...) for table '{0}'. 
  • trunk/framework/Data/ActiveRecord/TActiveRecord.php

    r1749 r1750  
    3636 * class UserRecord extends TActiveRecord 
    3737 * { 
     38 *     const TABLE='users'; //optional table name. 
     39 * 
    3840 *     public $username; //corresponds to the fieldname in the table 
    3941 *     public $email; 
    40  * 
    41  *     public static final $_tablename='users'; //optional table name. 
    4242 * 
    4343 *     //returns active record finder instance 
  • trunk/framework/Data/ActiveRecord/TActiveRecordGateway.php

    r1728 r1750  
    2626 
    2727        /** 
    28          * Property name for optional table name in TActiveRecord. 
    29          */ 
    30         const PROPERTY_TABLE_NAME='_tablename'; 
     28         * Constant name for specifying optional table name in TActiveRecord. 
     29         */ 
     30        const TABLE_CONST='TABLE'; 
    3131 
    3232        /** 
     
    4848 
    4949        /** 
    50          * Gets the table name from the $_tablename property of the active record 
     50         * Gets the table name from the 'TABLE' constant of the active record 
    5151         * class if defined, otherwise use the class name as table name. 
    5252         * @param TActiveRecord active record instance 
     
    5656        { 
    5757                $class = new ReflectionClass($record); 
    58                 if($class->hasProperty(self::PROPERTY_TABLE_NAME)) 
     58                if($class->hasConstant(self::TABLE_CONST)) 
    5959                { 
    60                         $value = $class->getProperty(self::PROPERTY_TABLE_NAME)->getValue(); 
    61                         if($value===null) 
     60                        $value = $class->getConstant(self::TABLE_CONST); 
     61                        if(empty($value)) 
    6262                                throw new TActiveRecordException('ar_invalid_tablename_property', 
    63                                         get_class($record),self::PROPERTY_TABLE_NAME); 
     63                                        get_class($record),self::TABLE_CONST); 
    6464                        return $value; 
    6565                } 
  • trunk/framework/prado-cli.php

    r1729 r1750  
    648648        { 
    649649                $props = implode("\n", $properties); 
    650                 $table = '$_tablename=\''.$tablename.'\''; 
    651650                $date = date('Y-m-d h:i:s'); 
    652651return <<<EOD 
     
    657656class $class extends TActiveRecord 
    658657{ 
    659         public static $table; 
     658        const TABLE='$tablename'; 
    660659 
    661660$props 
  • trunk/tests/simple_unit/ActiveRecord/records/DepSections.php

    r1729 r1750  
    66        public $order; 
    77 
    8         public static $_tablename='department_sections'; 
     8        const TABLE='department_sections'; 
    99 
    1010        public static function finder($className=__CLASS__) 
  • trunk/tests/simple_unit/ActiveRecord/records/DepartmentRecord.php

    r1729 r1750  
    88        public $order; 
    99 
    10         public static $_tablename = 'departments'; 
     10        const TABLE = 'departments'; 
    1111 
    1212        public static function finder($className=__CLASS__) 
  • trunk/tests/simple_unit/ActiveRecord/records/SimpleUser.php

    r1729 r1750  
    44        public $username; 
    55 
    6         public static $_tablename='simple_users'; 
     6        const TABLE='simple_users'; 
    77 
    88        public static function finder($className=__CLASS__) 
  • trunk/tests/simple_unit/ActiveRecord/records/SqliteUsers.php

    r1729 r1750  
    66        public $email; 
    77 
    8         public static $_tablename='users'; 
     8        const TABLE='users'; 
    99 
    1010        public static function finder($className=__CLASS__) 
  • trunk/tests/simple_unit/ActiveRecord/records/UserRecord.php

    r1729 r1750  
    1818        private $_level=-1; 
    1919 
    20         public static $_tablename='users'; 
     20        const TABLE='users'; 
    2121 
    2222        public function getLevel() 
  • trunk/tests/simple_unit/SqlMap/ActiveRecordSqlMapTest.php

    r1729 r1750  
    1515        public $Account_Cart_Option; 
    1616 
    17         private static $_tablename='Accounts'; 
     17        const TABLE='Accounts'; 
    1818 
    1919        public static function finder($className=__CLASS__)