tareq1988/wp-eloquent
Composer 安装命令:
composer require tareq1988/wp-eloquent
包简介
Eloquent ORM for WordPress
关键字:
README 文档
README
This is a library package to use Laravel's Eloquent ORM with WordPress.
Package Installation
To install this package, edit your composer.json file:
{ "require": { "tareq1988/wp-eloquent": "dev-master" } }
Now run:
$ composer install
Usage Example
Basic Usage
$db = \WeDevs\ORM\Eloquent\Database::instance(); var_dump( $db->table('users')->find(1) ); var_dump( $db->select('SELECT * FROM wp_users WHERE id = ?', [1]) ); var_dump( $db->table('users')->where('user_login', 'john')->first() ); // OR with DB facade use \WeDevs\ORM\Eloquent\Facades\DB; var_dump( DB::table('users')->find(1) ); var_dump( DB::select('SELECT * FROM wp_users WHERE id = ?', [1]) ); var_dump( DB::table('users')->where('user_login', 'john')->first() );
Creating Models For Custom Tables
You can use custom tables of the WordPress databases to create models:
<?php namespace Whatever; use WeDevs\ORM\Eloquent\Model; class CustomTableModel extends Model { /** * Name for table without prefix * * @var string */ protected $table = 'table_name'; /** * Columns that can be edited - IE not primary key or timestamps if being used */ protected $fillable = [ 'city', 'state', 'country' ]; /** * Disable created_at and update_at columns, unless you have those. */ public $timestamps = false; /** Everything below this is best done in an abstract class that custom tables extend */ /** * Set primary key as ID, because WordPress * * @var string */ protected $primaryKey = 'ID'; /** * Make ID guarded -- without this ID doesn't save. * * @var string */ protected $guarded = [ 'ID' ]; /** * Overide parent method to make sure prefixing is correct. * * @return string */ public function getTable() { // In this example, it's set, but this is better in an abstract class if ( isset( $this->table ) ){ $prefix = $this->getConnection()->db->prefix; return $prefix . $this->table; } return parent::getTable(); } }
Retrieving All Rows From A Table
$users = $db->table('users')->get(); foreach ($users as $user) { var_dump($user->display_name); }
Here users is the table name without prefix. The prefix will be applied automatically.
Other Examples
Writing a Model
use \WeDevs\ORM\Eloquent\Model as Model; class Employee extends Model { } var_dump( Employee::all()->toArray() ); // gets all employees var_dump( Employee::find(1) ); // find employee with ID 1
The class name Employee will be translated into PREFIX_employees table to run queries. But as usual, you can override the table name.
In-built Models for WordPress
- Post
- Comment
- Post Meta
- User
- User Meta
use WeDevs\ORM\WP\Post as Post; var_dump( Post::all() ); //returns only posts with WordPress post_type "post"
Filter Post by post_status and post_type
use WeDevs\ORM\WP\Post as Post; var_dump(Post::type('page')->get()->toArray()); // get pages var_dump(Post::status('publish')->get()->toArray()); // get posts with publish status var_dump(Post::type('page')->status('publish')->get()->toArray()); // get pages with publish status
How it Works
- Eloquent is mainly used here as the query builder
- WPDB is used to run queries built by Eloquent
- Hence, we have the benfit to use plugins like
debug-barorquery-monitorto get SQL query reporting. - It doesn't create any extra MySQL connection
Minimum Requirement
- PHP 5.6.4+
- WordPress 4.0+
Author
tareq1988/wp-eloquent 适用场景与选型建议
tareq1988/wp-eloquent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 55.22k 次下载、GitHub Stars 达 569, 最近一次更新时间为 2015 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「sql」 「plugin」 「wordpress」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tareq1988/wp-eloquent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tareq1988/wp-eloquent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tareq1988/wp-eloquent 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Kinikit - PHP Application development framework MVC component
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
Query filtering in your frontend
Database/ORM-agnostic query construction helper
PeskyORM - annoying ORM that contains tons of exceptions and throws them when anything goes wrong
统计信息
- 总下载量: 55.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 575
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2015-02-11