yiisoft / db
Database abstraction layer and query builder
Fund package maintenance!
Opencollective
yiisoft
Installs: 407 091
Dependents: 33
Suggesters: 3
Security: 0
Stars: 168
Watchers: 23
Forks: 41
Open Issues: 35
pkg:composer/yiisoft/db
Requires
- php: ^8.0
- ext-ctype: *
- ext-json: *
- ext-mbstring: *
- ext-pdo: *
- psr/log: ^2.0|^3.0
- psr/simple-cache: ^2.0|^3.0
Requires (Dev)
- maglnet/composer-require-checker: ^4.2
- phpunit/phpunit: ^9.6|^10.0
- rector/rector: ^1.0
- roave/infection-static-analysis-plugin: ^1.16
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.20
- yiisoft/aliases: ^3.0
- yiisoft/cache-file: ^3.1
- yiisoft/di: ^1.0
- yiisoft/event-dispatcher: ^1.0
- yiisoft/json: ^1.0
- yiisoft/log: ^2.0
- yiisoft/var-dumper: ^1.5
- yiisoft/yii-debug: dev-master|dev-php80
This package is auto-updated.
Last update: 2025-11-13 14:37:57 UTC
README
Yii Database
Framework-agnostic database abstraction layer that provides a set of classes to connect and interact with various database management systems (DBMS) using a unified API, including a powerful query builder.
Available database drivers:
- Yii DB MSSQL
- Yii DB MySQL (also supports MariaDB)
- Yii DB Oracle
- Yii DB PostgreSQL
- Yii DB SQLite
Optional packages that provide additional functionality:
- Yii Active Record provides an object-oriented interface for working with database tables, similar to ORM frameworks such as Doctrine or Hibernate.
Requirements
- PHP 8.1 - 8.4.
pdoPHP extension.
Installation
The package could be installed with Composer.
composer require yiisoft/db yiisoft/db-sqlite
Important
You must install yiisoft/db together with at least one database driver (e.g., yiisoft/db-mysql,
yiisoft/db-pgsql, yiisoft/db-sqlite, etc.) to actually connect to a database.
It also depends on PSR-16: Common Interface for Caching Libraries and requires the installation of PSR-16 implementation. For example, yiisoft/cache or one of the other cache handlers.
General Usage
To connect to a database, create an instance of the appropriate driver:
use Yiisoft\Db\Sqlite\Connection; use Yiisoft\Db\Sqlite\Driver; /** * @var Psr\SimpleCache\CacheInterface $cache */ // Creating a database connection $db = new Connection( new Driver('sqlite:memory:'), new SchemaCache($cache), );
You can then use the $db object to execute SQL queries, manage transactions, and perform other database operations.
Here are some examples:
use Yiisoft\Db\Connection\ConnectionInterface; /** * @var ConnectionInterface $db */ // Query builder $rows = $db ->select(['id', 'email']) ->from('{{%user}}') ->where(['last_name' => 'Smith']) ->limit(10) ->all(); // Insert $db->createCommand() ->insert( '{{%user}}', [ 'email' => 'mike@example.com', 'first_name' => 'Mike', 'last_name' => 'Smith', ], ) ->execute(); // Transaction $db->transaction( static function (ConnectionInterface $db) { $db->createCommand() ->update('{{%user}}', ['status' => 'active'], ['id' => 1]) ->execute(); $db->createCommand() ->update('{{%profile}}', ['visibility' => 'public'], ['user_id' => 1]) ->execute(); } )
Documentation
- Guide: English, Português - Brasil
- Internals
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii Database is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.