Database

Database & Read-Only Database services

The components under the Phalcon\Db namespace are the ones responsible for powering the Phalcon\Mvc\Model class - the Model in MVC for the framework. It consists of an independent high-level abstraction layer for database systems completely written in C.

This component allows for a lower level database manipulation than using traditional models.

Configurations

Database Settings

Define which adapter you want to use simply by setting the DATABASE_DRIVER environment variable. You can choose between mysql, postgresql and sqlight.

# Master Database using MySQL
DATABASE_ADAPTER=mysql
DATABASE_HOST=localhost
DATABASE_PORT=3306
DATABASE_DBNAME=database
DATABASE_USERNAME=username
DATABASE_PASSWORD=password
DATABASE_CHARSET=utf8

# Options
DATABASE_PDO_EMULATE_PREPARES=false
DATABASE_PDO_STRINGIFY_FETCHES=false
MYSQL_ATTR_SSL_VERIFY_SERVER_CERT=true

This component makes use of adapters to encapsulate specific database system details. Phalcon uses PDO to connect to databases. The following database engines are supported.

# Database Settings of the `mysql` driver
ANNOTATIONS_DRIVER=mysql
ANNOTATIONS_APCU_ADAPTER=\Phalcon\Db\Adapter\Pdo\Mysql

Usage

// if the class is aware of injections
$db = $this->db;

// if the container is present
$db = $this->di->get('db');

// to access the shared db service
$db = Di::getDefault()->get('db');

Sources

Last updated