From 087e49f8fe391a2d17b632240cbb892fcb0476d0 Mon Sep 17 00:00:00 2001 From: michaelw Date: Thu, 31 Jul 2014 10:28:55 +0100 Subject: [PATCH 1/2] Adds global configuration option Model::$short_table_names to allow namespace information to be disregarded when generating table names --- docs/configuration.rst | 24 ++++++++++++++++++++++++ docs/models.rst | 11 ++++++++--- paris.php | 38 ++++++++++++++++++++++++++++++++------ test/ParisTest53.php | 35 ++++++++++++++++++++++++++++++++--- 4 files changed, 96 insertions(+), 12 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index be228db4..cf989bb2 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -62,6 +62,30 @@ Here is a namespaced example to make it clearer: individual model classes. As documented in the :doc:`Models` section of the documentation. +Model prefixing +~~~~~~~~~~~~~~~ + +Setting: ``Model::$short_table_names`` + +Set as ``true`` to disregard namespace information when computing table names +from class names. + +By default the class ``\Models\CarTyre`` expects the table name ``models_car_tyre``. +With ``Model::$short_table_names = true`` the class ``\Models\CarTyre`` expects the +table name ``car_tyre``. + +.. code-block:: php + + find_many(); // SQL executed: SELECT * FROM `car_tyre` + + namespace Models { + class CarTyre extends Model { + + } + } Further Configuration ~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/models.rst b/docs/models.rst index 3816f379..95e9e54c 100644 --- a/docs/models.rst +++ b/docs/models.rst @@ -37,9 +37,14 @@ in a similar way. For example ``\Models\CarTyre`` would be converted to ``models_car_tyre``. Note here that backslashes are replaced with underscores in addition to the *CapWords* replacement discussed in the previous paragraph. -To disregard namespace information when calculating the table name, set a -**public static** property named ``$_table_use_short_name`` on your class. -This would result in ``\Models\CarTyre`` being converted to ``car_tyre``. +To disregard namespace information when calculating the table name, set +``Model::$short_table_names = true;``. Optionally this may be set or overridden at +class level with the **public static** property ``$_table_use_short_name``. The + +``$_table_use_short_name`` takes precedence over ``Model::$short_table_names`` +unless ``$_table_use_short_name`` is ``null`` (default). + +Either setting results in ``\Models\CarTyre`` being converted to ``car_tyre``. .. code-block:: php diff --git a/paris.php b/paris.php index beffed63..fe11d0f6 100644 --- a/paris.php +++ b/paris.php @@ -190,6 +190,17 @@ class Model { */ public static $auto_prefix_models = null; + /** + * Set true to to ignore namespace information when computing table names + * from class names. + * + * @example Model::$short_table_names = true; + * @example Model::$short_table_names = false; // default + * + * @var bool $short_table_names + */ + public static $short_table_names = false; + /** * The ORM instance used by this model * instance to communicate with the database. @@ -225,17 +236,18 @@ protected static function _get_static_property($class_name, $property, $default= * If not, the class name will be converted using * the _class_name_to_table_name method method. * - * If public static property $_table_use_short_name == true - * then $class_name passed to _class_name_to_table_name is - * stripped of namespace information. + * If Model::$short_table_names == true or public static + * property $_table_use_short_name == true then $class_name passed + * to _class_name_to_table_name is stripped of namespace information. * * @param string $class_name - * @return string + * +*@return string */ protected static function _get_table_name($class_name) { $specified_table_name = self::_get_static_property($class_name, '_table'); - $use_short_class_name = - self::_get_static_property($class_name, '_table_use_short_name'); + + $use_short_class_name = self::_use_short_table_name($class_name); if ($use_short_class_name) { $exploded_class_name = explode('\\', $class_name); @@ -248,6 +260,20 @@ protected static function _get_table_name($class_name) { return $specified_table_name; } + /** + * Should short table names, disregarding class namespaces, be computed? + * + * $class_property overrides $global_option, unless $class_property is null + * + * @param string $class_name + * @return bool + */ + protected static function _use_short_table_name($class_name) { + $global_option = self::$short_table_names; + $class_property = self::_get_static_property($class_name, '_table_use_short_name'); + return is_null($class_property) ? $global_option : $class_property; + } + /** * Convert a namespace to the standard PEAR underscore format. * diff --git a/test/ParisTest53.php b/test/ParisTest53.php index 4d067d66..c8cb8a68 100644 --- a/test/ParisTest53.php +++ b/test/ParisTest53.php @@ -19,15 +19,41 @@ public function tearDown() { ORM::set_db(null); } + + public function testNamespacedTableName() { Model::factory('Paris\Tests\Simple')->find_many(); $expected = 'SELECT * FROM `paris_tests_simple`'; $this->assertEquals($expected, ORM::get_last_query()); + + MustNotIgnoreNamespace::find_many(); + $expected = 'SELECT * FROM `paris_tests_must_not_ignore_namespace`'; + $this->assertEquals($expected, ORM::get_last_query()); + + Model::$short_table_names = true; + MustNotIgnoreNamespace::find_many(); + $expected = 'SELECT * FROM `paris_tests_must_not_ignore_namespace`'; + $this->assertEquals($expected, ORM::get_last_query()); + + Model::$short_table_names = false; + MustNotIgnoreNamespace::find_many(); + $expected = 'SELECT * FROM `paris_tests_must_not_ignore_namespace`'; + $this->assertEquals($expected, ORM::get_last_query()); } public function testIgnoredNamespaceTableName() { - IgnoreNamespace::find_many(); - $expected = 'SELECT * FROM `ignore_namespace`'; + MustIgnoreNamespace::find_many(); + $expected = 'SELECT * FROM `must_ignore_namespace`'; + $this->assertEquals($expected, ORM::get_last_query()); + + Model::$short_table_names = true; + MustIgnoreNamespace::find_many(); + $expected = 'SELECT * FROM `must_ignore_namespace`'; + $this->assertEquals($expected, ORM::get_last_query()); + + Model::$short_table_names = false; + MustIgnoreNamespace::find_many(); + $expected = 'SELECT * FROM `must_ignore_namespace`'; $this->assertEquals($expected, ORM::get_last_query()); } @@ -49,6 +75,9 @@ class Simple extends Model { } class ModelWithCustomTable extends Model { public static $_table = 'custom_table'; } -class IgnoreNamespace extends Model { +class MustIgnoreNamespace extends Model { public static $_table_use_short_name = true; } +class MustNotIgnoreNamespace extends Model { + public static $_table_use_short_name = false; +} From f7e6edb1f1112d158ebeb61ee4aee630d31f2065 Mon Sep 17 00:00:00 2001 From: michaelward82 Date: Thu, 31 Jul 2014 11:40:42 +0100 Subject: [PATCH 2/2] Extends test coverage for short table names config options --- test/ParisTest53.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/ParisTest53.php b/test/ParisTest53.php index c8cb8a68..0ef8091a 100644 --- a/test/ParisTest53.php +++ b/test/ParisTest53.php @@ -35,6 +35,11 @@ public function testNamespacedTableName() { $expected = 'SELECT * FROM `paris_tests_must_not_ignore_namespace`'; $this->assertEquals($expected, ORM::get_last_query()); + Model::$short_table_names = false; + MustUseGlobalNamespaceConfig::find_many(); + $expected = 'SELECT * FROM `paris_tests_must_use_global_namespace_config`'; + $this->assertEquals($expected, ORM::get_last_query()); + Model::$short_table_names = false; MustNotIgnoreNamespace::find_many(); $expected = 'SELECT * FROM `paris_tests_must_not_ignore_namespace`'; @@ -51,6 +56,11 @@ public function testIgnoredNamespaceTableName() { $expected = 'SELECT * FROM `must_ignore_namespace`'; $this->assertEquals($expected, ORM::get_last_query()); + Model::$short_table_names = true; + MustUseGlobalNamespaceConfig::find_many(); + $expected = 'SELECT * FROM `must_use_global_namespace_config`'; + $this->assertEquals($expected, ORM::get_last_query()); + Model::$short_table_names = false; MustIgnoreNamespace::find_many(); $expected = 'SELECT * FROM `must_ignore_namespace`'; @@ -81,3 +91,6 @@ class MustIgnoreNamespace extends Model { class MustNotIgnoreNamespace extends Model { public static $_table_use_short_name = false; } +class MustUseGlobalNamespaceConfig extends Model { + public static $_table_use_short_name = null; +}