gin0115 / pixie-wpdb
Pixie(由usmanhalalit创建)的WordPress适配版本,使用PDO的WPDB。
Requires
- php: >=7.1.0
- usmanhalalit/viocon: 1.0.*@dev
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3
- gin0115/wpunit-helpers: ~1.0.0
- php-stubs/wordpress-stubs: ^5.9.0
- phpmyadmin/sql-parser: dev-master
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^7.0 || ^8.0
- roots/wordpress: ^5.9
- symfony/var-dumper: 4.*
- szepeviktor/phpstan-wordpress: ^1.0
- wp-phpunit/wp-phpunit: ^5.9
- yoast/phpunit-polyfills: ^1.0.0
- dev-master
- 0.0.3
- 0.0.2
- 0.0.1
- 0.0.1-rc3
- 0.0.1-rc2
- 0.0.1-rc1
- dev-dependabot/composer/php-stubs/wordpress-stubs-tw-6.2.0
- dev-dependabot/composer/wp-phpunit/wp-phpunit-tw-6.2
- dev-dependabot/composer/roots/wordpress-tw-6.2
- dev-dependabot/composer/symfony/var-dumper-6.star
- dev-dependabot/composer/gin0115/wpunit-helpers-approx-1.1.1
- dev-move-update-to-v2
- dev-dev-v2
- dev-move-aggregate-to-v2
- dev-feature/gh56-move-statements-to-object-based
- dev-develop
- dev-feature/extend-tests-for-older-wp-versions
- dev-feature/gh36-introduce-when
- dev-feature/gh57-remove-container-dependency
- dev-feature/gh52-improve-updateOrInsert
- dev-feature/gh31-create-static-loader
- dev-feature/gh34-allow-raw-and-json-selector-in-aggregate-methods
- dev-feature/gh40-json-naming
- dev-feature/add-phpmyadmin-sql-parser
- dev-feature/gh14-pt3-json-join
- dev-feature/gh37-incorrect-fetchMode-usage
- dev-hotfix/add-php8-support
- dev-feature/gh14-pt2-where-clauses
- dev-feature/gh29-improve-order-by-tests
- dev-feature/gh8-configure-wpdb
- dev-feature/gh14-add-json-support--PT1-select-from-json
- dev-feature/gh13-extend-where
- dev-feature/gh10-findOrFail
- dev-feature/gh9-add-joinusing
This package is auto-updated.
Last update: 2024-08-30 01:17:55 UTC
README
Pixie WPDB是一个用于WordPress的查询构建器,也可以称为数据库抽象层。Pixie WPDB仅支持WPDB,并通过统一的API处理查询净化、表前缀和许多其他功能。
Pixie WPDB是由usmanhalalit最初编写的
pixie
的一个版本。现在pixie
已经不再积极开发。
特性
$thing = QB::table('someTable')->where('something','=', 'something else')->first();
安装
先决条件
- WordPress 5.7+(测试到5.9)
- PHP 7.1+(包括PHP8支持)
- MySql 5.7+或MariaDB 10.2+
- Composer(可选)
使用Composer
将Pixie包含到您的项目中最简单的方法是使用composer。
composer require gin0115/pixie-wpdb
静态加载器
如果您只想直接在插件中包含Pixie,您可以提取src
目录并将其添加到您的functions.php
或类似文件中。
require_once '/path/to/src/loader.php';
每个类都会检查是否已加载,以避免在多个插件中使用时发生冲突。
设置连接
如果您只计划有一个连接,您只需要配置一次连接。
# Basic setup // Access the global WPDB or a custom instance for additional tables. global $wpdb; // Configure the builder and/or internal WPDB instance $connection_config = [Connection::PREFIX => 'gin0115_']; // Give a *single* Alias $builder_alias = 'Gin0115\\DB'; new Connection( $wpdb, $connection_config, $builder_alias );
这将为使用此连接的QueryBuilder实例提供访问权限,通过定义的别名Gin0115\DB
$foos = Gin0115\DB::table('foo')->where('column', 'red')->get();
生成并执行的查询: "SELECT * FROM gin0115_foo WHERE column = 'red'; "
连接配置
您可以为查询构建器实例配置使用的连接。
值
$config = [ Connection::PREFIX => 'acme_', Connection::USE_WPDB_PREFIX => true, Connection::CLONE_WPDB => true, Connection::SHOW_ERRORS => false, ];
连接别名
当您创建一个连接时
new Connection($wpdb, $config, 'MyAlias');
MyAlias
是您想使用的类别名(如MyAlias::table(...)
),您可以使用任何名称(包括命名空间,如MyNamespace\\MyClass
),或者如果您不需要别名,也可以跳过它。别名让您能够轻松地在整个应用程序中访问QueryBuilder类。
用法
一旦创建了一个连接,就可以通过别名外观或创建一个实例来访问构建器。
静态用法
使用Pixie最简单的方法是使用提供的别名外观。这允许您在任何地方访问构建器实例,就像WPDB一样。
// Create the connection early on. $connection = new Connection($wpdb, $config, 'Alias'); // Insert some data to bar. Alias::table('bar')->insert(['column'=>'value']);
非静态用法
当不使用别名时,可以单独实例化QueryBuilder处理器,这对于依赖注入和测试非常有用。
// Create connection and builder instance. $connection = new Connection($wpdb, $config); $qb = new QueryBuilderHandler($connection); $query = $qb->table('my_table')->where('name', '=', 'Sana'); $results = $query->get();
$connection
是可选的,如果未提供,它将始终关联到第一个连接,但在您有多个数据库连接时非常有用。
致谢
这个包最初是基于Pixie的一个分支,由usmanhalalit编写。一些功能受到了Pecee-pixie分支和后续版本的影响,特别是扩展聚合方法。
变更日志
- 0.0.3 - 对
updateOrInsert()
方法的更多改进。 - 0.0.2 - 对
updateOrInsert()
方法的改进 - 0.0.1 - 对Muhammad Usman编写的初始代码进行了各种外部和内部修改
如果您发现任何错误,请编辑并发起一个pull request。
© 2022 Glynn Quelch. 在MIT许可下发布。