From 909dbdf29d1153c3abe1f7962e36a89c6dc348a6 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Thu, 22 Dec 2011 12:07:18 -0200 Subject: [PATCH] default NamingStrategy --- lib/Doctrine/ORM/DefaultNamingStrategy.php | 113 ++++++++++++++++ lib/Doctrine/ORM/NamingStrategy.php | 121 ++++++++++++++++++ .../Doctrine/Tests/ORM/NamingStrategyTest.php | 20 +++ 3 files changed, 254 insertions(+) create mode 100644 lib/Doctrine/ORM/DefaultNamingStrategy.php create mode 100644 lib/Doctrine/ORM/NamingStrategy.php create mode 100644 tests/Doctrine/Tests/ORM/NamingStrategyTest.php diff --git a/lib/Doctrine/ORM/DefaultNamingStrategy.php b/lib/Doctrine/ORM/DefaultNamingStrategy.php new file mode 100644 index 000000000..7e712a3bb --- /dev/null +++ b/lib/Doctrine/ORM/DefaultNamingStrategy.php @@ -0,0 +1,113 @@ +. + */ + +namespace Doctrine\ORM; + +/** + * The default NamingStrategy + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.3 + * @author Fabio B. Silva + */ +class DefaultNamingStrategy implements NamingStrategy +{ + + /** + * {@inheritdoc} + */ + public function classToTableName($className) + { + return $className; + } + + /** + * {@inheritdoc} + */ + public function propertyToColumnName($propertyName) + { + return $propertyName; + } + + /** + * {@inheritdoc} + */ + public function tableName($tableName) + { + return $tableName; + } + + /** + * {@inheritdoc} + */ + public function columnName($columnName) + { + return $columnName; + } + + /** + * {@inheritdoc} + */ + public function collectionTableName($ownerEntity, $ownerEntityTable, $associatedEntity, $associatedEntityTable, $propertyName) + { + return $propertyName; + } + + /** + * {@inheritdoc} + */ + public function joinKeyColumnName($joinedColumn, $joinedTable) + { + return $joinedColumn; + } + + /** + * {@inheritdoc} + */ + public function foreignKeyColumnName($propertyName, $propertyEntityName, $propertyTableName, $referencedColumnName) + { + return $propertyName ?: $propertyTableName; + } + + /** + * {@inheritdoc} + */ + public function logicalColumnName($columnName, $propertyName) + { + return $columnName ?: $propertyName; + } + + /** + * {@inheritdoc} + */ + public function logicalCollectionTableName($tableName, $ownerEntityTable, $associatedEntityTable, $propertyName) + { + return $ownerEntityTable . '_' . ( $associatedEntityTable ?: $propertyName ); + } + + /** + * {@inheritdoc} + */ + public function logicalCollectionColumnName($columnName, $propertyName, $referencedColumn) + { + return $columnName ?: ($propertyName . '_' . $referencedColumn); + } +} diff --git a/lib/Doctrine/ORM/NamingStrategy.php b/lib/Doctrine/ORM/NamingStrategy.php new file mode 100644 index 000000000..ff72be46f --- /dev/null +++ b/lib/Doctrine/ORM/NamingStrategy.php @@ -0,0 +1,121 @@ +. + */ + +namespace Doctrine\ORM; + +/** + * A set of rules for determining the physical column and table names + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.3 + * @author Fabio B. Silva + */ +interface NamingStrategy +{ + + /** + * Return a table name for an entity class + * + * @param string $className The fully-qualified class name + * @return string A table name + */ + function classToTableName($className); + + /** + * Return a column name for a property path expression + * + * @param string $propertyName A property path + * @return string A column name + */ + function propertyToColumnName($propertyName); + + /** + * Alter the table name given in the mapping document + * + * @param string tableName A table name + * @return string A table name + */ + function tableName($tableName); + + /** + * Alter the column name given in the mapping document + * + * @param string $columnName A column name + * @return string A column name + */ + function columnName($columnName); + + /** + * Return a collection table name ie an association having a join table + * + * @param string $ownerEntity + * @param string $ownerEntityTable Owner side table name + * @param string $associatedEntity + * @param string $associatedEntityTable Reverse side table name if any + * @param string $propertyName Collection role + */ + function collectionTableName($ownerEntity, $ownerEntityTable, $associatedEntity, $associatedEntityTable, $propertyName); + + /** + * Return the join key column name ie a FK column used in a JOINED strategy or for a secondary table + * + * @param string $joinedColumn Joined column name used to join with + * @param string $joinedTable Joined table name used to join with + */ + function joinKeyColumnName($joinedColumn, $joinedTable); + + /** + * Return the foreign key column name for the given parameters + * + * @param string $propertyName The property name involved + * @param string $propertyEntityName + * @param string $propertyTableName The property table name involved (logical one) + * @param string $referencedColumnName The referenced column name involved (logical one) + */ + function foreignKeyColumnName($propertyName, $propertyEntityName, $propertyTableName, $referencedColumnName); + + /** + * Return the logical column name used to refer to a column in the metadata + * + * @param string $columnName Given column name if any + * @param string $propertyName Property name of this column + */ + function logicalColumnName($columnName, $propertyName); + + /** + * Returns the logical collection table name used to refer to a table in the mapping metadata + * + * @param string $tableName The metadata explicit name + * @param string $ownerEntityTable Owner table entity table name (logical one) + * @param string $associatedEntityTable Reverse side table name if any (logical one) + * @param string $propertyName Collection role + */ + function logicalCollectionTableName($tableName, $ownerEntityTable, $associatedEntityTable, $propertyName); + + /** + * Returns the logical foreign key column name used to refer to this column in the mapping metadata + * + * @param string $columnName Given column name in the metadata if any + * @param string $propertyName Property name + * @param string $referencedColumn Referenced column name in the join + */ + function logicalCollectionColumnName($columnName, $propertyName, $referencedColumn); +} diff --git a/tests/Doctrine/Tests/ORM/NamingStrategyTest.php b/tests/Doctrine/Tests/ORM/NamingStrategyTest.php new file mode 100644 index 000000000..9bfa9afba --- /dev/null +++ b/tests/Doctrine/Tests/ORM/NamingStrategyTest.php @@ -0,0 +1,20 @@ +assertEquals('ShortClassName', $strategy->classToTableName('ShortClassName')); + } +} \ No newline at end of file