Installing TDBM in Lumen

Lumen 5.x integration

Installation

To install TDBM in Lumen 5.x:

composer require thecodingmachine/tdbm-laravel ^5.0
There is no typo here. The thecodingmachine/tdbm-laravel can be used both for Laravel and Lumen integration.

Then edit your bootstrap/app.php file and register these 2 service providers:

$app->register(Nayjest\LaravelDoctrineDBAL\ServiceProvider::class);
$app->register(TheCodingMachine\TDBM\Laravel\Providers\TdbmServiceProvider::class);
The Nayjest\LaravelDoctrineDBAL\ServiceProvider provides a "Doctrine DBAL connection" needed by TDBM. This connection reuses the default database connection used by Lumen.

Generating beans and DAOs

When installation is done, you need to generate DAOs and beans from your data model.

Run the following command:

php artisan tdbm:generate
You must run this command after the installation of the package, and each time you run a migration (i.e. each time the database model changes).

Advanced configuration

By default, TDBM will write DAOs in the App\Daos namespace and beans in the App\Beans namespace. If you want to customize this, you can edit the bootstrap/app.php file:

config([
    'database.tdbm.daoNamespace' => 'App\\Daos',
    'database.tdbm.beanNamespace' => 'App\\Beans'    
]);

Accessing DAOs through a route closure

In Lumen, you would typically inject the DAOs in your route closure.

bootstrap/app.php

use App\Daos\UserDao;

// ...

$app->get('test', function(UserDao $userDao) {
    $user = $this->userDao->getById($id);
    // do stuff
});

Accessing DAOs through a Controller

Alternatively, if you use controllers, you can also inject the DAOs in your controllers constructor (or in any class resolved by the Lumen container).

Typically:

bootstrap/app.php

$app->get('test', [
    'uses' => 'TestController@index'
]);

app/Http/Controllers/TestController.php

<?php
namespace App\Http\Controllers;

use App\Daos\MigrationDao;
use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * @var UserDao
     */
    private $userDao;

    /**
     * The DAO we need is injected in the constructor
     */
    public function __construct(UserDao $userDao)
    {
        $this->userDao = $userDao;
    }

    public function index($id)
    {
        $user = $this->userDao->getById($id);
        // do stuff
    }
}

Next step

Let's now learn how to access the database.

Found a typo? Something is wrong in this documentation? Just fork and edit it!