yiisoft / data-db
yiisoft/data 数据提供者的数据库查询适配器
dev-master / 3.0.x-dev
2024-09-21 14:08 UTC
Requires
- php: ^8.1
- yiisoft/data: dev-master
- yiisoft/db: ^1.3
Requires (Dev)
- maglnet/composer-require-checker: ^4.2
- phpunit/phpunit: ^10.5
- rector/rector: ^1.0
- roave/infection-static-analysis-plugin: ^1.16
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^5.21
- vlucas/phpdotenv: ^5.6
- yiisoft/cache: ^3.0
- yiisoft/db-mssql: ^1.2
- yiisoft/db-mysql: ^1.2
- yiisoft/db-oracle: ^1.3
- yiisoft/db-pgsql: ^1.3
- yiisoft/db-sqlite: ^1.2
This package is auto-updated.
Last update: 2024-09-21 14:17:01 UTC
README
Yii 数据 DB
该包为通用数据抽象提供 Yiisoft\Db\Query\Query
绑定。
需求
- PHP 8.1 或更高版本。
安装
可以使用 Composer 安装此包
composer require yiisoft/data-db
通用用法
use Yiisoft\Data\Db\Filter\All; use Yiisoft\Data\Db\Filter\Equals; use Yiisoft\Data\Db\QueryDataReader; $typeId = filter_input(INPUT_GET, 'type_id', FILTER_VALIDATE_INT); $countryId = filter_input(INPUT_GET, 'country_id', FILTER_VALIDATE_INT); $parentId = filter_input(INPUT_GET, 'parent_id', FILTER_VALIDATE_INT); // OR // $typeId = $_GET['type_id'] ?? null; // $countryId = $_GET['country_id'] ?? null; // $parentId = $_GET['parent_id'] ?? null; // OR // $params = $request->getQueryParams(); // $typeId = $params['type_id'] ?? null; // $countryId = $params['country_id'] ?? null; // $parentId = $params['parent_id'] ?? null; // OR same with ArrayHelper::getValue(); $query = $arFactory->createQueryTo(AR::class); $filter = new All( (new Equals('type_id', $typeId)), (new Equals('country_id', $countryId)), (new Equals('parent_id', $parentId)) ); $dataReader = (new QueryDataReader($query)) ->withFilter($filter);
如果 $typeId, $countryId 和 $parentId 等于 NULL,则生成的 SQL 如下
SELECT AR::tableName().* FROM AR::tableName() WHERE type_id IS NULL AND country_id IS NULL AND parent_id IS NULL
如果我们想忽略不存在的参数(即未在 $_GET/queryParams 中设置),则可以使用 withIgnoreNull(true) 方法
$typeId = 1; $countryId = null; $parentId = null; $filter = new All( (new Equals('type_id', $typeId))->withIgnoreNull(true), (new Equals('country_id', $countryId))->withIgnoreNull(true), (new Equals('parent_id', $parentId))->withIgnoreNull(true) ); $dataReader = (new QueryDataReader($query)) ->withFilter($filter);
生成的 SQL 如下
SELECT AR::tableName().* FROM AR::tableName() WHERE type_id = 1
如果查询通过相同的列名连接多个表,请将表名作为第三个过滤参数传递
$equalsTableOne = (new Equals('id', 1, 'table_one'))->withIgnoreNull(true); $equalsTableTwo = (new Equals('id', 100, 'table_two'))->withIgnoreNull(true);
当前过滤器/处理器
比较
- 等于 - =
- 不等于 - !=
- 大于 - >
- 大于等于 - >=
- 在
- 小于 - <
- 小于等于 - <=
- 不是
- 类似\ILIke
- 存在
- 介于
过滤器 "Like" 或 "ILike"
此过滤器具有以下方法 withBoth
, withoutBoth
, withStart
, withoutStart
, withEnd
, withoutEnd
$filter = new Like('column', 'value'); $dataReader = (new QueryDataReader($query))->withFilter($filter); //column LIKE '%value%' $filter = (new Like('column', 'value'))->withoutStart(); $dataReader = (new QueryDataReader($query))->withFilter($filter); //column LIKE 'value%' $filter = (new Like('column', 'value'))->withoutEnd(); $dataReader = (new QueryDataReader($query))->withFilter($filter); //column LIKE '%value'
过滤器 "Exists"
仅接受一个类型为 Yiisoft\Db\Query\Query
的参数
过滤器 "Not"
仅接受一个类型为 Yiisoft\Data\Reader\Filter\FilterInterface
的参数
分组
- 所有 - and
- 任何 - or
文档
如果您需要帮助或有任何问题,请访问 Yii 论坛。您也可以查看其他 Yii 社区资源。
许可证
Yii 数据 DB 是免费软件。它根据 BSD 许可证发布。有关更多信息,请参阅 LICENSE
。
由 Yii 软件 维护。