summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxue <>2007-03-06 20:40:51 +0000
committerxue <>2007-03-06 20:40:51 +0000
commit8a674fb83fa2dd80bc653745e03b24450a9cf68d (patch)
tree83f72026185075b0ffacf5c1996d3424b0e2f31f
parent2ab695a553abf26e530f5e97c1ea357233d80998 (diff)
changed the way to specify active record table.
-rw-r--r--UPGRADE14
-rw-r--r--demos/address-book/protected/pages/AddressRecord.php2
-rw-r--r--demos/chat/protected/App_Code/ChatBufferRecord.php4
-rw-r--r--demos/chat/protected/App_Code/ChatUserRecord.php4
-rw-r--r--demos/quickstart/protected/controls/Comments/CommentBlock.php4
-rw-r--r--demos/quickstart/protected/pages/Database/ActiveRecord.page16
-rw-r--r--demos/quickstart/protected/pages/Database/Samples/Scaffold/Home.php4
-rw-r--r--demos/quickstart/protected/pages/Database/Scaffold.page4
-rw-r--r--demos/quickstart/protected/pages/Database/SqlMap.page6
-rw-r--r--demos/quickstart/protected/pages/Tutorial/AjaxChat.page8
-rw-r--r--demos/quickstart/protected/pages/Tutorial/fr/AjaxChat.page8
-rw-r--r--framework/Data/ActiveRecord/Exceptions/messages.txt2
-rw-r--r--framework/Data/ActiveRecord/TActiveRecord.php4
-rw-r--r--framework/Data/ActiveRecord/TActiveRecordGateway.php14
-rwxr-xr-xframework/prado-cli.php3
-rw-r--r--tests/simple_unit/ActiveRecord/records/DepSections.php2
-rw-r--r--tests/simple_unit/ActiveRecord/records/DepartmentRecord.php2
-rw-r--r--tests/simple_unit/ActiveRecord/records/SimpleUser.php2
-rw-r--r--tests/simple_unit/ActiveRecord/records/SqliteUsers.php2
-rw-r--r--tests/simple_unit/ActiveRecord/records/UserRecord.php2
-rw-r--r--tests/simple_unit/SqlMap/ActiveRecordSqlMapTest.php2
21 files changed, 58 insertions, 51 deletions
diff --git a/UPGRADE b/UPGRADE
index 9415e94f..9e27ba0b 100644
--- a/UPGRADE
+++ b/UPGRADE
@@ -11,14 +11,22 @@ for both A and B.
Upgrading from v3.1a
---------------------
-- The signature of TActiveRecord::finder() is changed. All TActiveRecord-descendant
- classes that override this method will be affected. Please use the
- following code to override the method:
+- The signature of TActiveRecord::finder() is changed. This affects
+ all TActiveRecord-descendant classes that override this method.
+ Please use the following code to override the method:
public static function finder($className=__CLASS__)
{
return parent::finder($className);
}
+- The way to specify the table name for an active record class is changed.
+ Previously, it used the static class member '_tablename'.
+ Now it uses class constant as follows:
+ class UserRecord extends TActiveRecord
+ {
+ const TABLE='users_table';
+ }
+
Upgrading from v3.0.x
---------------------
- Validators ClientSide.OnSuccess becomes ClientSide.OnValidationSuccess,
diff --git a/demos/address-book/protected/pages/AddressRecord.php b/demos/address-book/protected/pages/AddressRecord.php
index 55c4ba06..aaf8db7c 100644
--- a/demos/address-book/protected/pages/AddressRecord.php
+++ b/demos/address-book/protected/pages/AddressRecord.php
@@ -4,7 +4,7 @@
*/
class AddressRecord extends TActiveRecord
{
- public static $_tablename='addresses';
+ const TABLE='addresses';
/**
* @var integer $id
diff --git a/demos/chat/protected/App_Code/ChatBufferRecord.php b/demos/chat/protected/App_Code/ChatBufferRecord.php
index 03f94704..f4d53db0 100644
--- a/demos/chat/protected/App_Code/ChatBufferRecord.php
+++ b/demos/chat/protected/App_Code/ChatBufferRecord.php
@@ -2,14 +2,14 @@
class ChatBufferRecord extends TActiveRecord
{
+ const TABLE='chat_buffer';
+
public $id;
public $for_user;
public $from_user;
public $message;
private $_created_on;
- public static $_tablename='chat_buffer';
-
public function getCreated_On()
{
if($this->_created_on === null)
diff --git a/demos/chat/protected/App_Code/ChatUserRecord.php b/demos/chat/protected/App_Code/ChatUserRecord.php
index e5ebc761..b68fbd4d 100644
--- a/demos/chat/protected/App_Code/ChatUserRecord.php
+++ b/demos/chat/protected/App_Code/ChatUserRecord.php
@@ -2,11 +2,11 @@
class ChatUserRecord extends TActiveRecord
{
+ const TABLE='chat_users';
+
public $username;
private $_last_activity;
- public static $_tablename='chat_users';
-
public function getLast_Activity()
{
if($this->_last_activity === null)
diff --git a/demos/quickstart/protected/controls/Comments/CommentBlock.php b/demos/quickstart/protected/controls/Comments/CommentBlock.php
index b0f23d47..9c008491 100644
--- a/demos/quickstart/protected/controls/Comments/CommentBlock.php
+++ b/demos/quickstart/protected/controls/Comments/CommentBlock.php
@@ -10,6 +10,8 @@ $manager->setDbConnection($db);
class CommentRecord extends TActiveRecord
{
+ const TABLE='qs_comments';
+
public $id;
public $username;
public $date_added;
@@ -17,8 +19,6 @@ class CommentRecord extends TActiveRecord
public $block_id;
public $content;
- public static $_tablename='qs_comments';
-
public static function finder($className=__CLASS__)
{
return parent::finder($className);
diff --git a/demos/quickstart/protected/pages/Database/ActiveRecord.page b/demos/quickstart/protected/pages/Database/ActiveRecord.page
index 041a1126..0a8d6580 100644
--- a/demos/quickstart/protected/pages/Database/ActiveRecord.page
+++ b/demos/quickstart/protected/pages/Database/ActiveRecord.page
@@ -73,11 +73,11 @@ CREATE TABLE users
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690148">
class UserRecord extends TActiveRecord
{
+ const TABLE='users'; //table name
+
public $username; //the column named "username" in the "users" table
public $email;
- public static $_tablename='users'; //table name
-
/**
* @return TActiveRecord active record finder instance
*/
@@ -89,15 +89,15 @@ class UserRecord extends TActiveRecord
</com:TTextHighlighter>
</p>
<p id="690485" class="block-content">Each property of the <tt>UserRecord</tt> class must correspond to a
- column with the same name in the "users" table. The static class variable
- <tt>$_tablename</tt> (must be public) is optional when the class name is the same as
- the table name in the database, otherwise <tt>$_tablename</tt> must
+ column with the same name in the "users" table. The class constant
+ <tt>TABLE</tt> is optional when the class name is the same as
+ the table name in the database, otherwise <tt>TABLE</tt> must
specify the table name that corresponds to your Active Record class.
</p>
<div class="note"><b class="note">Note:</b>
-You may need to quote (specific to your database) the value of the <tt>$_tablename</tt>.
-E.g. MySQL uses back-ticks, <tt>$_tablename = "`database1`.`table1`"</tt>
+You may need to quote (specific to your database) the value of the <tt>TABLE</tt>.
+E.g. MySQL uses back-ticks, <tt>TABLE = "`database1`.`table1`"</tt>
</div>
<p class="block-content" id="ar_as_component">
@@ -120,7 +120,7 @@ class UserRecord extends TActiveRecord {
</com:TTextHighlighter>
<div class="info"><b class="note">Info:</b>
-<tt>TActiveRecord</tt> can also work with database views by specifying the value <tt>$_tablename</tt>
+<tt>TActiveRecord</tt> can also work with database views by specifying the constant <tt>TABLE</tt>
corresponding to the view name. However, objects returned
from views are read-only, calling the <tt>save()</tt> or <tt>delete()</tt> method
will raise an exception.
diff --git a/demos/quickstart/protected/pages/Database/Samples/Scaffold/Home.php b/demos/quickstart/protected/pages/Database/Samples/Scaffold/Home.php
index 9bfe3f6d..45d72f1f 100644
--- a/demos/quickstart/protected/pages/Database/Samples/Scaffold/Home.php
+++ b/demos/quickstart/protected/pages/Database/Samples/Scaffold/Home.php
@@ -5,12 +5,12 @@ Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldView');
class AddressRecord extends TActiveRecord
{
+ const TABLE='addresses';
+
public $id;
public $username;
public $phone;
- public static $_tablename='addresses';
-
//for demo, we use static db here
//otherwise we should use TActiveRecordConfig in application.xml
private static $_db;
diff --git a/demos/quickstart/protected/pages/Database/Scaffold.page b/demos/quickstart/protected/pages/Database/Scaffold.page
index 36a0ec21..055cac26 100644
--- a/demos/quickstart/protected/pages/Database/Scaffold.page
+++ b/demos/quickstart/protected/pages/Database/Scaffold.page
@@ -39,10 +39,10 @@ table as defined in the <a href="?page=Database.ActiveRecord">Active Record</a>
<com:TTextHighlighter Language="php" CssClass="source">
class UserRecord extends TActiveRecord
{
+ const TABLE='users';
+
public $username;
public $email;
-
- public static $_tablename='users';
}
</com:TTextHighlighter>
diff --git a/demos/quickstart/protected/pages/Database/SqlMap.page b/demos/quickstart/protected/pages/Database/SqlMap.page
index c8ced852..a4082527 100644
--- a/demos/quickstart/protected/pages/Database/SqlMap.page
+++ b/demos/quickstart/protected/pages/Database/SqlMap.page
@@ -213,11 +213,11 @@ $user = $sqlmap->queryForObject("SelectUsers");
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_700173">
class UserRecord extends TActiveRecord
{
+ const TABLE='users'; //table name
+
public $username; //the column named "username" in the "users" table
public $email;
-
- private static $_tablename='users'; //table name
-
+
/**
* @return TActiveRecord active record finder instance
*/
diff --git a/demos/quickstart/protected/pages/Tutorial/AjaxChat.page b/demos/quickstart/protected/pages/Tutorial/AjaxChat.page
index 7d978a12..4e40b33a 100644
--- a/demos/quickstart/protected/pages/Tutorial/AjaxChat.page
+++ b/demos/quickstart/protected/pages/Tutorial/AjaxChat.page
@@ -168,11 +168,11 @@ as <tt>App_Code/chat.db</tt>.
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_90033">
class ChatUserRecord extends TActiveRecord
{
+ const TABLE='chat_users';
+
public $username;
public $last_activity;
- public static $_tablename='chat_users';
-
public static function finder($className=__CLASS__)
{
return parent::finder($className);
@@ -502,14 +502,14 @@ The corresponding <tt>ChatBufferRecord</tt> class is saved as
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_90045">
class ChatBufferRecord extends TActiveRecord
{
+ const TABLE='chat_buffer';
+
public $id;
public $for_user;
public $from_user;
public $message;
private $_created_on;
- public static $_tablename='chat_buffer';
-
public function getCreated_On()
{
if($this->_created_on === null)
diff --git a/demos/quickstart/protected/pages/Tutorial/fr/AjaxChat.page b/demos/quickstart/protected/pages/Tutorial/fr/AjaxChat.page
index 7bc36d5f..24c5ea76 100644
--- a/demos/quickstart/protected/pages/Tutorial/fr/AjaxChat.page
+++ b/demos/quickstart/protected/pages/Tutorial/fr/AjaxChat.page
@@ -168,11 +168,11 @@ as <tt>App_Code/chat.db</tt>.
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_90033">
class ChatUserRecord extends TActiveRecord
{
+ const TABLE='chat_users';
+
public $username;
public $last_activity;
- public static $_tablename='chat_users';
-
public static function finder($className=__CLASS__)
{
return parent::finder($className);
@@ -502,14 +502,14 @@ The corresponding <tt>ChatBufferRecord</tt> class is saved as
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_90045">
class ChatBufferRecord extends TActiveRecord
{
+ const TABLE='chat_buffer';
+
public $id;
public $for_user;
public $from_user;
public $message;
private $_created_on;
- public static $_tablename='chat_buffer';
-
public function getCreated_On()
{
if($this->_created_on === null)
diff --git a/framework/Data/ActiveRecord/Exceptions/messages.txt b/framework/Data/ActiveRecord/Exceptions/messages.txt
index 84b58fe5..9ab1693e 100644
--- a/framework/Data/ActiveRecord/Exceptions/messages.txt
+++ b/framework/Data/ActiveRecord/Exceptions/messages.txt
@@ -9,7 +9,7 @@ ar_no_primary_key_found = Table '{0}' does not contain any primary key fiel
ar_primary_key_is_scalar = Primary key '{1}' in table '{0}' is NOT a composite key, invalid value '{2} used.
ar_invalid_db_connection = Missing or invalid default database connection for ActiveRecord class '{0}', default connection is set by the DbConnection property of TActiveRecordManager.
ar_mismatch_args_exception = ActiveRecord finder method '{0}' expects {1} parameters but found only {2} parameters instead.
-ar_invalid_tablename_property = ActiveRecord tablename property '{0}::${1}' must be static and not null.
+ar_invalid_tablename_property = Constant {0}::{1} must be a valid database table name.
ar_value_must_not_be_null = Property '{0}::${2}' must not be null as defined by column '{2}' in table '{1}'.
ar_missing_pk_values = Missing primary key values in forming IN(key1, key2, ...) for table '{0}'.
ar_pk_value_count_mismatch = Composite key value count mismatch in forming IN( (key1, key2, ..), (key3, key4, ..)) for table '{0}'.
diff --git a/framework/Data/ActiveRecord/TActiveRecord.php b/framework/Data/ActiveRecord/TActiveRecord.php
index 4e06f645..cbf02058 100644
--- a/framework/Data/ActiveRecord/TActiveRecord.php
+++ b/framework/Data/ActiveRecord/TActiveRecord.php
@@ -35,11 +35,11 @@ Prado::using('System.Data.ActiveRecord.TActiveRecordCriteria');
* <code>
* class UserRecord extends TActiveRecord
* {
+ * const TABLE='users'; //optional table name.
+ *
* public $username; //corresponds to the fieldname in the table
* public $email;
*
- * public static final $_tablename='users'; //optional table name.
- *
* //returns active record finder instance
* public static function finder($className=__CLASS__)
* {
diff --git a/framework/Data/ActiveRecord/TActiveRecordGateway.php b/framework/Data/ActiveRecord/TActiveRecordGateway.php
index c925f3c9..f9cc5bbd 100644
--- a/framework/Data/ActiveRecord/TActiveRecordGateway.php
+++ b/framework/Data/ActiveRecord/TActiveRecordGateway.php
@@ -25,9 +25,9 @@ class TActiveRecordGateway extends TComponent
private $_tables=array(); //meta data cache.
/**
- * Property name for optional table name in TActiveRecord.
+ * Constant name for specifying optional table name in TActiveRecord.
*/
- const PROPERTY_TABLE_NAME='_tablename';
+ const TABLE_CONST='TABLE';
/**
* Record gateway constructor.
@@ -47,7 +47,7 @@ class TActiveRecordGateway extends TComponent
}
/**
- * Gets the table name from the $_tablename property of the active record
+ * Gets the table name from the 'TABLE' constant of the active record
* class if defined, otherwise use the class name as table name.
* @param TActiveRecord active record instance
* @return string table name for the given record class.
@@ -55,12 +55,12 @@ class TActiveRecordGateway extends TComponent
public function getTableName(TActiveRecord $record)
{
$class = new ReflectionClass($record);
- if($class->hasProperty(self::PROPERTY_TABLE_NAME))
+ if($class->hasConstant(self::TABLE_CONST))
{
- $value = $class->getProperty(self::PROPERTY_TABLE_NAME)->getValue();
- if($value===null)
+ $value = $class->getConstant(self::TABLE_CONST);
+ if(empty($value))
throw new TActiveRecordException('ar_invalid_tablename_property',
- get_class($record),self::PROPERTY_TABLE_NAME);
+ get_class($record),self::TABLE_CONST);
return $value;
}
else
diff --git a/framework/prado-cli.php b/framework/prado-cli.php
index 8ede48ab..c5cb7d58 100755
--- a/framework/prado-cli.php
+++ b/framework/prado-cli.php
@@ -647,7 +647,6 @@ EOD;
protected function generateClass($properties, $tablename, $class)
{
$props = implode("\n", $properties);
- $table = '$_tablename=\''.$tablename.'\'';
$date = date('Y-m-d h:i:s');
return <<<EOD
<?php
@@ -656,7 +655,7 @@ return <<<EOD
*/
class $class extends TActiveRecord
{
- public static $table;
+ const TABLE='$tablename';
$props
diff --git a/tests/simple_unit/ActiveRecord/records/DepSections.php b/tests/simple_unit/ActiveRecord/records/DepSections.php
index c172245c..7b213c33 100644
--- a/tests/simple_unit/ActiveRecord/records/DepSections.php
+++ b/tests/simple_unit/ActiveRecord/records/DepSections.php
@@ -5,7 +5,7 @@ class DepSections extends TActiveRecord
public $section_id;
public $order;
- public static $_tablename='department_sections';
+ const TABLE='department_sections';
public static function finder($className=__CLASS__)
{
diff --git a/tests/simple_unit/ActiveRecord/records/DepartmentRecord.php b/tests/simple_unit/ActiveRecord/records/DepartmentRecord.php
index be0f5fd1..61307826 100644
--- a/tests/simple_unit/ActiveRecord/records/DepartmentRecord.php
+++ b/tests/simple_unit/ActiveRecord/records/DepartmentRecord.php
@@ -7,7 +7,7 @@ class DepartmentRecord extends TActiveRecord
public $active;
public $order;
- public static $_tablename = 'departments';
+ const TABLE = 'departments';
public static function finder($className=__CLASS__)
{
diff --git a/tests/simple_unit/ActiveRecord/records/SimpleUser.php b/tests/simple_unit/ActiveRecord/records/SimpleUser.php
index 3112e203..4ff0b982 100644
--- a/tests/simple_unit/ActiveRecord/records/SimpleUser.php
+++ b/tests/simple_unit/ActiveRecord/records/SimpleUser.php
@@ -3,7 +3,7 @@ class SimpleUser extends TActiveRecord
{
public $username;
- public static $_tablename='simple_users';
+ const TABLE='simple_users';
public static function finder($className=__CLASS__)
{
diff --git a/tests/simple_unit/ActiveRecord/records/SqliteUsers.php b/tests/simple_unit/ActiveRecord/records/SqliteUsers.php
index 19940bc0..ffa47c72 100644
--- a/tests/simple_unit/ActiveRecord/records/SqliteUsers.php
+++ b/tests/simple_unit/ActiveRecord/records/SqliteUsers.php
@@ -5,7 +5,7 @@ class SqliteUsers extends TActiveRecord
public $password;
public $email;
- public static $_tablename='users';
+ const TABLE='users';
public static function finder($className=__CLASS__)
{
diff --git a/tests/simple_unit/ActiveRecord/records/UserRecord.php b/tests/simple_unit/ActiveRecord/records/UserRecord.php
index 6ef0c637..c3cb78b2 100644
--- a/tests/simple_unit/ActiveRecord/records/UserRecord.php
+++ b/tests/simple_unit/ActiveRecord/records/UserRecord.php
@@ -17,7 +17,7 @@ class UserRecord extends TActiveRecord
private $_level=-1;
- public static $_tablename='users';
+ const TABLE='users';
public function getLevel()
{
diff --git a/tests/simple_unit/SqlMap/ActiveRecordSqlMapTest.php b/tests/simple_unit/SqlMap/ActiveRecordSqlMapTest.php
index 1f018a02..63d62534 100644
--- a/tests/simple_unit/SqlMap/ActiveRecordSqlMapTest.php
+++ b/tests/simple_unit/SqlMap/ActiveRecordSqlMapTest.php
@@ -14,7 +14,7 @@ class ActiveAccount extends TActiveRecord
public $Account_Banner_Option;
public $Account_Cart_Option;
- private static $_tablename='Accounts';
+ const TABLE='Accounts';
public static function finder($className=__CLASS__)
{