title | subTitle | currentMenu |
---|---|---|
Configuring the naming of beans and DAOs |
configuring_naming |
By default, if you have a users
table, TDBM will generate those classes:
User
class for the main beanAbstractUser
class for the "base" beanUserDao
class for the main DAOAbstractUserDao
class for the "base" DAO
Note: before TDBM 4.3, naming of beans was quite different:
UserBean
class for the main beanUserBaseBean
class for the "base" beanUserDao
class for the main DAOUserBaseDao
class for the "base" DAO
These naming can be configured. To generate those names, TDBM relies on a naming strategy.
The default naming strategy is a class named DefaultNamingStrategy
and implementing the NamingStrategyInterface
.
When configuring the naming, you have 2 solutions:
- configure the default
DefaultNamingStrategy
instance - provide your own
NamingStrategyInterface
implementation to TDBM
- Name of getters
- Name of setters
- Name of find-by-index methods
- Name of JSON serialized properties
The default naming strategy assumes the name of the tables are in English and in plural form. The default naming strategy will put the table name in CamelCase, in singular form and then add suffixes and prefixes.
Those suffixes and prefixes are configurable:
// Let's create a naming strategy that maps behaviour of TDBM version <= 4.2
$strategy = new DefaultNamingStrategy();
$strategy->setBeanPrefix('');
$strategy->setBeanSuffix('Bean');
$strategy->setBaseBeanPrefix('');
$strategy->setBaseBeanSuffix('BaseBean');
$strategy->setDaoPrefix('');
$strategy->setDaoSuffix('Dao');
$strategy->setBaseDaoPrefix('');
$strategy->setBaseDaoSuffix('BaseDao');
Furthermore, you can configure a set of exceptions. This can be useful if your table names are not in English or not in plural form.
Let's assume you have a table named chevaux
('horses' in French). The singular form is 'cheval', so you would want a 'Cheval' bean and a 'ChevalDao'. That easy with the setExceptions
method:
$strategy->setExceptions([
'chevaux' => 'Cheval'
]);
(Available in TDBM 5.1+)
You can also use the @Bean annotation to directly alter the name of the beans/daos.
CREATE TABLE `members` (
`id` varchar(36) NOT NULL,
`login` varchar(50),
PRIMARY KEY (`id`)
) COMMENT("@Bean(name=\"User\")");
If you need a more fine-grained control over the naming strategy, you can simply implement your own NamingStrategyInterface
class. Or you can extend the AbstractNamingStrategy
class that implements most of the boilerplate code you will need and still offers a large degree of freedom.
The naming strategy is passed as a parameter of the Configuration
class used to configure the TDBMService
.