Follow the configuration instructions here.
Once DBAL component configured properly, you can access your databases in controllers and services multiple ways:
namespace App\Controller;
use Cycle\Database\DatabaseManager;
class HomeController
{
public function index(DatabaseManager $dbal): void
{
//Default database
dump($dbal->database());
//Default database over shortcut
dump($this->db);
//Using alias default which points to primary database
dump($dbal->database('default'));
//Secondary
dump($dbal->database('slave'));
//Short binding + database name
dump($this->dbal->database('secondary'));
}
}
DBAL component fully support IoC injections based on database name and their aliases:
public function index(Database $database, Database $primary, Database $slave): void
{
//Database is an alias for "primary"
dump($database === $primary);
dump($primary);
dump($slave);
}
Access Cycle\Database\DatabaseProviderInterface
and default database instance using PrototypeTrait
:
namespace App\Controller;
use Cycle\Prototype\Traits\PrototypeTrait;
class HomeController
{
use PrototypeTrait;
public function index(): void
{
dump($this->dbal);
dump($this->db); // default db
}
}
To run the database query use method query
:
dump(
$db->query(
'SELECT * FROM users WHERE id > ?',
[
1
]
)->fetchAll()
);
To execute update or delete statement use the alternative method execute
:
dump(
$db->execute(
'DELETE FROM users WHERE id > ?',
[
1
]
) // number of affected rows
);
Note
Read how to use query builders here.