summaryrefslogtreecommitdiff
path: root/framework/Data
diff options
context:
space:
mode:
authormikl <>2008-10-16 08:46:57 +0000
committermikl <>2008-10-16 08:46:57 +0000
commit104100cccd4e7b30ec84ed055fb78745dd94b5b0 (patch)
tree3d4cef9ed43053dd7d0a861ec32119523ee10e75 /framework/Data
parent710a1d43a1ff0f5449529a13d9a93676fbfb3f5d (diff)
Ticket #891 (new static method table() in TActiveRecord)
Diffstat (limited to 'framework/Data')
-rw-r--r--framework/Data/ActiveRecord/Exceptions/messages.txt3
-rw-r--r--framework/Data/ActiveRecord/TActiveRecord.php14
-rw-r--r--framework/Data/ActiveRecord/TActiveRecordGateway.php12
3 files changed, 27 insertions, 2 deletions
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;
* }
* </code>
* 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');
* }
* </code>
*
+ * Since v3.1.3 you can also define a static method that returns the table name.
+ * <code>
+ * class UserRecord extends TActiveRecord
+ * {
+ * public static function table()
+ * {
+ * return 'users';
+ * }
+ *
+ * }
+ * </code>
+ *
* @author Wei Zhuo <weizho[at]gmail[dot]com>
* @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));