yiisoft / data-db
Database query adapter for Yii Data
Fund package maintenance!
Opencollective
yiisoft
Installs: 3 369
Dependents: 0
Suggesters: 0
Security: 0
Stars: 14
Watchers: 14
Forks: 3
Open Issues: 2
pkg:composer/yiisoft/data-db
Requires
- php: 8.1 - 8.5
- yiisoft/data: ^2.0
- yiisoft/db: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.92.0
- maglnet/composer-require-checker: ^4.7.1
- phpunit/phpunit: ^10.5.60
- rector/rector: ^2.2.14
- roave/infection-static-analysis-plugin: ^1.35
- spatie/phpunit-watcher: ^1.24
- vimeo/psalm: ^5.26.1 || ^6.9.1
- vlucas/phpdotenv: ^5.6.2
- yiisoft/db-mssql: ^2.0
- yiisoft/db-mysql: ^2.0
- yiisoft/db-oracle: ^2.0
- yiisoft/db-pgsql: ^2.0
- yiisoft/db-sqlite: ^2.0
- yiisoft/psr-dummy-provider: ^1.0.2
- yiisoft/test-support: ^3.1
This package is auto-updated.
Last update: 2025-12-13 17:24:35 UTC
README
Yii Data DB
The package provides data reader implementation based on Yii DB query and a set of DB-specific filters.
Detailed build statuses:
| RDBMS | Status |
|---|---|
| SQLite | |
| MySQL | |
| PostgreSQL | |
| Microsoft SQL Server | |
| Oracle |
Requirements
- PHP 8.1 - 8.5.
Installation
The package could be installed with Composer:
composer require yiisoft/data-db
General usage
The QueryDataReader wraps a database query to provide a flexible data reading interface:
use Yiisoft\Data\Db\QueryDataReader; use Yiisoft\Data\Reader\Filter\Equals; use Yiisoft\Data\Reader\Filter\GreaterThan; use Yiisoft\Data\Reader\Filter\Like; use Yiisoft\Data\Reader\Filter\AndX; use Yiisoft\Data\Reader\Sort; use Yiisoft\Db\Query\Query; $query = (new Query($db))->from('customer'); $dataReader = new QueryDataReader($query); // Iterate through results foreach ($dataReader->read() as $customer) { // ... process each customer ... } // Read a single record $customer = $dataReader->readOne(); // Get total count $total = $dataReader->count(); // Sorting $sort = Sort::any(['name', 'email'])->withOrderString('-name,email'); $dataReader = $dataReader->withSort($sort); // Filtering $filter = new AndX( new Equals('status', 'active'), new GreaterThan('age', 18), new Like('name', 'John') ); $dataReader = $dataReader->withFilter($filter); // Pagination $dataReader = $dataReader ->withOffset(20) ->withLimit(10);
Field mapping
Map data reader field names to database columns:
use Yiisoft\Data\Db\QueryDataReader; use Yiisoft\Data\Reader\Filter\Equals; $dataReader = new QueryDataReader( query: $query, fieldMapper: [ 'userName' => 'user_name', 'createdAt' => 'created_at', ] ); // Now you can filter and sort by 'userName' and it will use 'user_name' column $filter = new Equals('userName', 'admin');
Batch processing
Process large datasets in batches to reduce memory usage:
use Yiisoft\Data\Db\QueryDataReader; $dataReader = new QueryDataReader($query); $dataReader = $dataReader->withBatchSize(100); foreach ($dataReader->read() as $item) { // Items are fetched in batches of 100 }
Documentation
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 Data DB is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.