From 104100cccd4e7b30ec84ed055fb78745dd94b5b0 Mon Sep 17 00:00:00 2001 From: mikl <> Date: Thu, 16 Oct 2008 08:46:57 +0000 Subject: Ticket #891 (new static method table() in TActiveRecord) --- HISTORY | 7 ++++--- .../quickstart/protected/pages/Database/ActiveRecord.page | 15 ++++++++++++++- framework/Data/ActiveRecord/Exceptions/messages.txt | 3 ++- framework/Data/ActiveRecord/TActiveRecord.php | 14 +++++++++++++- framework/Data/ActiveRecord/TActiveRecordGateway.php | 12 ++++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/HISTORY b/HISTORY index c2395664..2e631a39 100644 --- a/HISTORY +++ b/HISTORY @@ -27,24 +27,25 @@ CHG: Ticket#844 - Upgraded TinyMCE to 3.1.0.1 (Christophe) CHG: Ticket#917 - TUrlMapping - change members to protected (Michael) CHG: Ticket#919 - TUserManager loadUserData function (Michael) ENH: Workaround for slow meta data retrieval in MySQL<5.1.21 (Michael) +ENH: Ticket#756 - TDateFormat & TNumberFormat - allow settings default text when Value isn't set. (Carl) ENH: Ticket#823 - PromptText/PromptValue only populated if there is data (Knut) ENH: Ticket#876 - Assign empty string to CssUrl on TTabPanel to avoid loading extra css (GoDZilla, Knut) ENH: Ticket#890 - Minor optimization: Use $var===null over is_null($var) (Knut) ENH: Ticket#893 - Added page parameter to queryForPagedList() to specify the initial page to load (Michael) ENH: Ticket#896 - TTheme - enhance for subclassing (Knut) ENH: Ticket#898 - Minor optimization: Use (int) over intval() (Knut) -ENH: Ticket#901 - Using TDbDataReader directly as a DataSource of TDataBoundControl's like TDataGrid (Knut) -ENH: Ticket#911 - prado-cli: Better error message if database connection fails when generating Active Record skeletons (Knut) ENH: Ticket#809 - "LIMIT 1" for ActiveRecord find() and findBy() (Carl) ENH: Ticket#848 - TCache "set" and "add" with empty values (Carl) -ENH: Ticket#756 - TDateFormat & TNumberFormat - allow settings default text when Value isn't set. (Carl) ENH: Ticket#822 - Not receiving emails from TEmailLogRoute (Carl) +ENH: Ticket#901 - Using TDbDataReader directly as a DataSource of TDataBoundControl's like TDataGrid (Knut) +ENH: Ticket#911 - prado-cli: Better error message if database connection fails when generating Active Record skeletons (Knut) ENH: Ticket#913 - PRADO Copyright notice in HTML source (Carl) NEW: Added Prado.Validation.validateControl(id) on client side to validate a specific control (Michael) NEW: Added MessageSource_Database to I18N (uses TDbConnection) (Michael) NEW: Ticket#935 - Add TDatePicker (Brad, Christophe) NEW: Ticket#857 - Added Authentication expiration support to TAuthManager (Michael) NEW: Ticket#853 - Add Drag and drop components (Christophe) +NEW: Ticket#891 - Added method that returns table name to TActiveRecord (Michael) Version 3.1.2 April 21, 2008 ============================ diff --git a/demos/quickstart/protected/pages/Database/ActiveRecord.page b/demos/quickstart/protected/pages/Database/ActiveRecord.page index 70998587..d54290ab 100644 --- a/demos/quickstart/protected/pages/Database/ActiveRecord.page +++ b/demos/quickstart/protected/pages/Database/ActiveRecord.page @@ -129,6 +129,19 @@ class UserRecord extends TActiveRecord You may specify qualified table names. E.g. for MySQL, TABLE = "`database1`.`table1`". +
Note: +Since version 3.1.3 you can also use a static method to define the table name. +This allows you to dynamically specify which table should be used by the ActiveRecord. + +class TeamRecord extends TActiveRecord +{ + public static function table() { + return 'Teams'; + } +} + +
+

Since TActiveRecord extends TComponent, setter and getter methods can be defined to allow control over how variables @@ -1183,4 +1196,4 @@ instead of $userRecord->first_name. This helps separation of logic and -

$Id$
\ No newline at end of file +
$Id$
diff --git a/framework/Data/ActiveRecord/Exceptions/messages.txt b/framework/Data/ActiveRecord/Exceptions/messages.txt index fabfc1a4..0702c840 100644 --- a/framework/Data/ActiveRecord/Exceptions/messages.txt +++ b/framework/Data/ActiveRecord/Exceptions/messages.txt @@ -10,6 +10,7 @@ ar_primary_key_is_scalar = Primary key '{1}' in table '{0}' is NOT a composi 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 = Constant {0}::{1} must be a valid database table name. +ar_invalid_tablename_method = Method {0}::{1} must return 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}'. @@ -21,4 +22,4 @@ ar_invalid_criteria = Invalid criteria object, must be a string or instanc ar_relations_undefined = Unable to determine Active Record relationships because static array property {0}::${1} is not defined. ar_undefined_relation_prop = Unable to find {1}::${2}['{0}'], Active Record relationship definition for property "{0}" not found in entries of {1}::${2}. ar_invalid_relationship = Invalid active record relationship. -ar_relations_missing_fk = Unable to find foreign key relationships in table '{0}' that corresponds to table '{1}'. \ No newline at end of file +ar_relations_missing_fk = Unable to find foreign key relationships in table '{0}' that corresponds to table '{1}'. diff --git a/framework/Data/ActiveRecord/TActiveRecord.php b/framework/Data/ActiveRecord/TActiveRecord.php index 4e6fd134..f3932849 100644 --- a/framework/Data/ActiveRecord/TActiveRecord.php +++ b/framework/Data/ActiveRecord/TActiveRecord.php @@ -79,7 +79,7 @@ Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelationContext'); * 'email_address'=>'email', * ); * public $username; - * pulbic $email; + * public $email; * } * * In the above, the 'users' table consists of 'user_id' and 'email_address' columns, @@ -129,6 +129,18 @@ Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelationContext'); * } * * + * Since v3.1.3 you can also define a static method that returns the table name. + * + * class UserRecord extends TActiveRecord + * { + * public static function table() + * { + * return 'users'; + * } + * + * } + * + * * @author Wei Zhuo * @version $Id$ * @package System.Data.ActiveRecord diff --git a/framework/Data/ActiveRecord/TActiveRecordGateway.php b/framework/Data/ActiveRecord/TActiveRecordGateway.php index 23104c00..112ec3e8 100644 --- a/framework/Data/ActiveRecord/TActiveRecordGateway.php +++ b/framework/Data/ActiveRecord/TActiveRecordGateway.php @@ -31,6 +31,10 @@ class TActiveRecordGateway extends TComponent * Constant name for specifying optional table name in TActiveRecord. */ const TABLE_CONST='TABLE'; + /** + * Method name for returning optional table name in in TActiveRecord + */ + const TABLE_METHOD='table'; /** * Record gateway constructor. @@ -65,6 +69,14 @@ class TActiveRecordGateway extends TComponent throw new TActiveRecordException('ar_invalid_tablename_property', get_class($record),self::TABLE_CONST); return $value; + } + elseif ($class->hasMethod(self::TABLE_METHOD)) + { + $value = call_user_func(array(get_class($record),self::TABLE_METHOD)); + if(empty($value)) + throw new TActiveRecordException('ar_invalid_tablename_method', + get_class($record),self::TABLE_METHOD); + return $value; } else return strtolower(get_class($record)); -- cgit v1.2.3