Revision: Thu, 26 Sep 2024 15:47:52 GMT
v2.14 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.14
Edit this page

Database - Query Builders

You can read how to work with Database using manually written queries here.

DBAL component includes a set of query builders used to unify the way of working with different databases and simplify migration to different DBMS over the lifetime of the application.

Before we start

To demonstrate query building abilities let's declare sample table in our default database first:

php
namespace App\Controller;

use Cycle\Database\Database;

class HomeController
{
    public function index(Database $db): void
    {
        $schema = $db->table('test')->getSchema();

        $schema->primary('id');
        $schema->datetime('time_created');
        $schema->enum('status', ['active', 'disabled'])->defaultValue('active');
        $schema->string('name', 64);
        $schema->string('email');
        $schema->double('balance');
        $schema->save();
    }
}

Note
You can read more about declaring database schemas here.

Note
Full documentation about using Query builder you can find here.