Installation & Setup
Setting up a Symfony Application
While Smalldb does not require Symfony or any other framework, it will help us with building the application.
First, start with a skeleton:
$ composer create-project symfony/skeleton hello-world-todo-list Creating a "symfony/skeleton" project at "./hello-world-todo-list" Installing symfony/skeleton (v5.1.99) ... $ cd hello-world-todo-list
To make the @Route
annotation work, we will need to install the annotation support.
Also, we need to install the Symfony Form component and Twig template engine.
$ composer require annotations symfony/form twig
Once everything is installed, we should be able to start the skeleton:
$ php -S localhost:8000 -t public/ PHP 7.4.5 Development Server (http://localhost:8000) started
Then, we can visit http://localhost:8000/ and see the “Welcome to Symfony” page. So let’s leave the server running and continue with installing Smalldb.
Installing Smalldb
Simply install smalldb/libsmalldb
package,
and the Symfony bundle smalldb/smalldb-symfony-bundle
:
$ composer require smalldb/libsmalldb smalldb/smalldb-symfony-bundle
For a better experience we will also install the Symfony Profiler:
$ composer require profiler
Now, a new toolbar should appear in our application and clicking the red “404” should open the Profiler. One of the items in the Profiler’s menu should be “Smalldb”. If you can see it, the skeleton is ready.
Creating a Database
In this small application, we will use an SQLite database of a single table:
-- var/database.sql
CREATE TABLE "Tasks" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"description" TEXT NOT NULL,
"completedAt" INTEGER NULL
);
INSERT INTO Tasks (description, completedAt)
VALUES
("Get some chocolate", CURRENT_TIMESTAMP),
("Water the plants", NULL),
("Go for a walk", NULL);
Store the SQL script in var/database.sql
and create a new SQLite database:
$ sqlite3 var/database.db < var/database.sql
The database should contain the table of Tasks:
$ sqlite3 -header -column var/database.db 'SELECT * FROM Tasks' id description completedAt ---------- ------------------ ------------------- 1 Get some chocolate 2020-08-09 13:50:52 2 Water the plants 3 Go for a walk
To make the database available in the application, install doctrine/dbal
and configure the database.
To avoid installing Doctrine ORM, we create the config file in advance:
# config/packages/doctrine.yaml
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
Then, we can install Doctrine DBAL and the related bundle:
$ composer require doctrine/dbal doctrine/doctrine-bundle
Finally, we shall configure the database location by setting the DATABASE_URL
variable
in the .env
file to sqlite:///%kernel.project_dir%/var/database.db
:
$ sed -i 's|^\(DATABASE_URL\)=.*|\1=sqlite:///%kernel.project_dir%/var/database.db|' .env