Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves issue #88. Allows static calling of Model subclasses, ignoring namespace info during table name generation. #90

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions docs/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,19 @@ 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 override this default behaviour, add a **public static** property to
your class called ``$_table``:
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``.

.. code-block:: php

<?php
class User extends Model {
public static $_table_use_short_name = true;
}

To override the default naming behaviour and directly specify a table name,
add a **public static** property to your class called ``$_table``:

.. code-block:: php

Expand Down Expand Up @@ -73,4 +84,4 @@ called ``$_id_column``:
**Note** - Paris has its *own* default ID column name mechanism, and
does not respect column names specified in Idiorm’s configuration.

.. _Active Record pattern: http://martinfowler.com/eaaCatalog/activeRecord.html
.. _Active Record pattern: http://martinfowler.com/eaaCatalog/activeRecord.html
17 changes: 16 additions & 1 deletion paris.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,26 @@ protected static function _get_static_property($class_name, $property, $default=
* Static method to get a table name given a class name.
* If the supplied class has a public static property
* named $_table, the value of this property will be
* returned. If not, the class name will be converted using
* returned.
*
* 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.
*
*/
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');

if ($use_short_class_name) {
$exploded_class_name = explode('\\', $class_name);
$class_name = end($exploded_class_name);
}

if (is_null($specified_table_name)) {
return self::_class_name_to_table_name($class_name);
}
Expand Down
9 changes: 9 additions & 0 deletions test/ParisTest53.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public function testNamespacedTableName() {
$this->assertEquals($expected, ORM::get_last_query());
}

public function testIgnoredNamespaceTableName() {
IgnoreNamespace::find_many();
$expected = 'SELECT * FROM `ignore_namespace`';
$this->assertEquals($expected, ORM::get_last_query());
}

public function testModelWithCustomTable() {
Model::factory('ModelWithCustomTable')->find_many();
$expected = 'SELECT * FROM `custom_table`';
Expand All @@ -43,3 +49,6 @@ class Simple extends Model { }
class ModelWithCustomTable extends Model {
public static $_table = 'custom_table';
}
class IgnoreNamespace extends Model {
public static $_table_use_short_name = true;
}