To install TDBM in Silex 2:
composer require thecodingmachine/tdbm-silex ^5.0
Then edit your src/app.php
file and register these 3 service providers:
// Let's register the database connection
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_mysql',
'dbname' => 'my_db',
'host' => 'localhost',
'user' => 'my_user',
'password' => 'my_password'
),
));
// Let's register the Doctrine cache
$app->register(new \CHH\Silex\CacheServiceProvider, [
'cache.options' => [
'default' => [
// Customize this to another cache driver if you don't have APCu installed
'driver' => \Doctrine\Common\Cache\ApcuCache::class,
],
],
]);
// Finally, let's register TDBM
$app->register(new \TheCodingMachine\TDBM\Silex\Providers\TdbmServiceProvider(), [
'tdbm.daoNamespace' => 'App\Daos',
'tdbm.beanNamespace' => 'App\Beans'
]);
Now, we need to register a command in the Silex console.
Assuming you started from the silex-skeleton package, you should edit the src/console.php
file.
$console->add(new \TheCodingMachine\TDBM\Commands\GenerateCommand($app['tdbm.configuration']));
Note: if you do not have a console (because you started from a blank Silex project), you can use the KnpLabs' Console Service Provider to add support for the Symfony console and then, register the TheCodingMachine\TDBM\Commands\GenerateCommand
command provided by TDBM.
When installation is done, you need to generate DAOs and beans from your data model.
Run the following command:
bin/console tdbm:generate
The TDBM service provider will create one service per DAO.
The service name is the short class name with the first character in lower case.
For instance:
In Silex, you will typically access the DAOs fomr the $app
variable passed in the closure:
$app->get('test', function($app) {
$user = $app['userDao']->getById($id);
// do stuff
});
Let's now learn how to access the database.
Found a typo? Something is wrong in this documentation? Just fork and edit it!