You are browsing a version that has not yet been released. |
Implementing a NamingStrategy
Using a naming strategy you can provide rules for generating database identifiers,
column or table names. This feature helps
reduce the verbosity of the mapping document, eliminating repetitive noise (eg: TABLE_
).
Configuring a naming strategy
The default strategy used by Doctrine is quite minimal.
By default the Doctrine\ORM\Mapping\DefaultNamingStrategy
uses the simple class name and the attribute names to generate tables and columns.
You can specify a different strategy by calling Doctrine\ORM\Configuration#setNamingStrategy()
:
Underscore naming strategy
\Doctrine\ORM\Mapping\UnderscoreNamingStrategy
is a built-in strategy.
For SomeEntityName the strategy will generate the table SOME_ENTITY_NAME with the
CASE_UPPER
option, or some_entity_name with the CASE_LOWER
option.
Naming strategy interface
The interface Doctrine\ORM\Mapping\NamingStrategy
allows you to specify
a naming strategy for database tables and columns.
1 <?php
/**
* 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
*
* @param string $propertyName A property
* @return string A column name
*/
function propertyToColumnName($propertyName);
/**
* Return the default reference column name
*
* @return string A column name
*/
function referenceColumnName();
/**
* Return a join column name for a property
*
* @param string $propertyName A property
* @return string A join column name
*/
function joinColumnName($propertyName, $className = null);
/**
* Return a join table name
*
* @param string $sourceEntity The source entity
* @param string $targetEntity The target entity
* @param string $propertyName A property
* @return string A join table name
*/
function joinTableName($sourceEntity, $targetEntity, $propertyName = null);
/**
* Return the foreign key column name for the given parameters
*
* @param string $entityName A entity
* @param string $referencedColumnName A property
* @return string A join column name
*/
function joinKeyColumnName($entityName, $referencedColumnName = null);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Implementing a naming strategy
If you have database naming standards, like all table names should be prefixed by the application prefix, all column names should be lower case, you can easily achieve such standards by implementing a naming strategy.
You need to create a class which implements Doctrine\ORM\Mapping\NamingStrategy
.
1 <?php
class MyAppNamingStrategy implements NamingStrategy
{
public function classToTableName($className)
{
return 'MyApp_' . substr($className, strrpos($className, '\\') + 1);
}
public function propertyToColumnName($propertyName)
{
return $propertyName;
}
public function referenceColumnName()
{
return 'id';
}
public function joinColumnName($propertyName, $className = null)
{
return $propertyName . '_' . $this->referenceColumnName();
}
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
{
return strtolower($this->classToTableName($sourceEntity) . '_' .
$this->classToTableName($targetEntity));
}
public function joinKeyColumnName($entityName, $referencedColumnName = null)
{
return strtolower($this->classToTableName($entityName) . '_' .
($referencedColumnName ?: $this->referenceColumnName()));
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30