dbout/wp-orm
Composer 安装命令:
composer require dbout/wp-orm
包简介
WordPress ORM with Eloquent.
README 文档
README
WordPress ORM with Eloquent is a small library that adds a basic ORM into WordPress, which is easily extendable and includes models for core WordPress models such as posts, post metas, users, comments and more.
The ORM is based on Eloquent ORM and uses the WordPress connection (wpdb class).
Tip
To simplify the integration of this library, we recommend using WordPress with one of the following tools: Bedrock, Themosis or Wordplate.
Features
- ✅ Support core WordPress models:
Comment,Option,Post,Term,TermTaxonomy,TermRelationship,User,PostMetaandUserMeta - ✅ Support core WordPress post types:
Article,AttachmentandPage - ✅ Based on core WordPress database connection (
wpdbclass), no configuration required - ✅ Custom functions to filter models with meta
- ✅ Meta casting (e.g. Attribute Casting)
- ❤️ Easy integration of a custom post and comment type
- ❤️ Easy model creation for projects with custom tables
- ❤️ All the features available in Eloquent are usable with this library
Not yet developed but planned in a future version:
- 🗓️ Create migration tool with Eloquent
- 🗓️ Multisite support (network-shared tables and
switch_blog()handling)
Documentation
This documentation only covers the specific points of this library, if you want to know more about Eloquent, the easiest is to look at the documentation of Eloquent.
You can find all the documentation in the wiki.
Installation
Requirements
This package targets a stricter runtime than WordPress itself:
- PHP >= 8.3
- WordPress >= 6.3
- Composer
Installation
You can use Composer. Follow the installation instructions if you do not already have composer installed.
composer require dbout/wp-orm
In your wp-config.php make sure you include the autoloader:
require __DIR__ . '/vendor/autoload.php';
🎉 You have nothing more to do, you can use the library now. No need to configure database accesses because the wpdb connection is used.
Quick start
Once installed, every model is ready to use without any configuration. Here are the most common patterns:
Retrieve a model
use Dbout\WpOrm\Models\Post; use Dbout\WpOrm\Models\User; $post = Post::find(42); $post = Post::findOneByName('hello-world'); $user = User::findOneByEmail('john@example.com');
Query with the builder
use Dbout\WpOrm\Enums\PostStatus; use Dbout\WpOrm\Models\Post; $publishedPosts = Post::query() ->whereStatus(PostStatus::Publish) ->whereTypes('post', 'page') ->orderBy(Post::DATE, 'desc') ->limit(10) ->get();
Create or update a model
use Dbout\WpOrm\Models\Post; $post = new Post(); $post->setPostTitle('Hello world'); $post->setPostName('hello-world'); $post->setPostType('post'); $post->save(); $post->setPostTitle('Hello, again'); $post->save();
Work with metas
$post->setMeta('color', 'blue'); $value = $post->getMetaValue('color'); // 'blue' $post->hasMeta('color'); // true $post->deleteMeta('color'); // Filter posts by meta value Post::query() ->addMetaToFilter('color', 'blue') ->addMetaToSelect('size') ->get();
Use relations
$post = Post::find(42); $author = $post->author; // BelongsTo User $comments = $post->comments; // HasMany Comment $parent = $post->parent; // BelongsTo Post (self)
For everything else (eager loading, scopes, transactions, casts…), see the Eloquent documentation — every Eloquent feature works out of the box.
Security notes
Warning
Mass assignment is wide open by default.
Every model inherits protected $guarded = [], which means every column is mass-assignable. A call like User::create($_POST) would let a caller set sensitive fields such as user_pass. When you accept user input, always pre-validate it or override $fillable / $guarded on the model:
class SafeUser extends \Dbout\WpOrm\Models\User { protected $fillable = [ self::LOGIN, self::EMAIL, self::DISPLAY_NAME, ]; }
Warning
Multisite is not supported in this release.
The library does not handle network-shared tables or switch_blog(). After a switch_blog() call, the connection prefix is not refreshed and models targeting shared tables (User, UserMeta) may produce incorrect queries on subsites. Multisite support is planned for a future release — track the milestone for progress.
Testing
🐞 This project includes two types of tests:
- Unit tests - Isolated tests without WordPress dependencies
- WordPress tests - Integration tests with WordPress core (uses
wp-phpunit/wp-phpunit)
Both suites run on PHPUnit 12.
Running tests:
# Unit tests composer run test:unit # WordPress tests (requires Docker) ./run-wp-tests.sh # With coverage ./run-wp-tests.sh --coverage
Local setup:
WordPress tests require Docker and Subversion. The run-wp-tests.sh script automatically sets up a MySQL container and installs WordPress test environment. WordPress files are cached in var/testings/ for faster subsequent runs.
See TESTING.md for detailed setup instructions and troubleshooting.
Contributing
💕 🦄 We encourage you to contribute to this repository, so everyone can benefit from new features, bug fixes, and any other improvements. Have a look at our contributing guidelines to find out how to raise a pull request.
dbout/wp-orm 适用场景与选型建议
dbout/wp-orm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.25k 次下载、GitHub Stars 达 129, 最近一次更新时间为 2020 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「sql」 「db」 「wordpress」 「migration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dbout/wp-orm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dbout/wp-orm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dbout/wp-orm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Query filtering in your frontend
统计信息
- 总下载量: 10.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 129
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-01