Revision: Fri, 17 May 2024 13:03:33 GMT
v1.0 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.12
Edit this page

PDO Queries

DBAL component is based on PDO extension. You have full access to PDO functionality including ability to write custom SQL queries and work with PDOStatements.

Query Database

You can query your database using prepared PDO statement following way:

php
$statement = $this->db->query('SELECT COUNT(*) FROM users WHERE id > ?', [1]);

assert($statement instanceof \PDOStatement);
dump($statement);

Named parameters are also supported:

php
$statement = $this->db->query('SELECT COUNT(*) FROM users WHERE id > :id', [
    ':id' => 2
]);

assert($statement instanceof \PDOStatement);
dump($statement);

Use Query Builders to let DBAL compile DBMS specific query for you.

Access PDO

If you wish to access PDO instance associated with database use:

php
dump($this->db->getDriver()->getPDO());