Skip to content

Adding a Database

Add a database to a new project or to a project that exists, with make:database or the --database option.

crest new --database gives a new project a database (see The database). For a project that exists, make:database does the same, in each flavor. The project needs crest.php; for a project with no crest.php, use crest init --database (see Adding Crest to an existing project):

crest make:database sqlite

Crest changes no file that exists. It writes the new files, and prints the lines to add to the files that exist. For SQLite, in an MVC project:

Created src/Providers/DbProvider.php
Created storage/database.sqlite
Created storage/.gitkeep
Nothing registers it yet. Add it to providers() in your front controller:

    new \App\Providers\DbProvider($this->projectRoot),

In a project with another front controller, register it with
$di->register(new \App\Providers\DbProvider($this->projectRoot)).

Add to .gitignore:

    /storage/database.sqlite

Add to .env:

    # The database: a SQLite file, relative to the project root. PHP on the host
    # does not read this file: there, the default in DbProvider applies.
    DB_NAME=storage/database.sqlite

Then crest make:model <table> reads it.
File What Crest does
DbProvider.php in the provider path writes it, from the stub that new uses: for MVC a Phalcon\Db adapter, for ADR a DataMapper Connection with the alias db
storage/database.sqlite and storage/.gitkeep (SQLite) writes each file that does not exist. A database file that exists stays, also with --force
the front controller prints the line that registers DbProvider. In an ADR project, it prints the registerProviders() override, or only the line to add when the front has the override (as make:provider does). With no front controller (no bootstrap key), it prints the line for the container of the project
the compose file (server database) prints the service database with its volume, and depends_on for the app service. When the compose file has a database service, Crest prints a note in place of the service
the Dockerfile (server database) prints the lines that install the PDO extension
.gitignore (SQLite) prints the line to add
.env prints the lines to add. For a server database, only when the project has a compose file

The compose file is the first of compose.yaml, compose.yml, docker-compose.yaml and docker-compose.yml in the project. Without one, Crest prints no compose, Dockerfile or .env lines for a server database: nothing gives .env to the app, and DB_HOST names a compose service. Crest prints that PHP needs the PDO extension, and that the defaults of DbProvider connect to 127.0.0.1 and the default port. When the app service of crest.php is named database, make:database stops, as new does.

The DB_NAME and DB_USERNAME defaults of a server database come from the folder name of the project, as new uses the name of the project. When DbProvider.php exists, make:database stops, and writes nothing. --force writes DbProvider again. make:database none stops: there is no database to add.

When the provider folder has no DbProvider.php, make:database also starts the front controller of the bootstrap key, if it can, and stops when its container has a db service:

crest: the project container has a 'db' service already; make:database adds no DbProvider

A front controller that does not start, for example in a project with no vendor/ folder, does not stop make:database. With DbProvider.php, the check of the file decides: DbProvider gives the db service itself.

With no front controller, Crest prints the line for the container of the project, and no line for a front controller. For SQLite, $projectRoot is the root folder of the project:

Nothing registers it yet. crest.php names no front controller, so register it
in the DI of the project:

    $di->register(new \App\Providers\DbProvider($projectRoot));

In an ADR project, the line is (new \App\Provider\DbProvider($projectRoot))->provide($container);.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close