wenprise / eloquent
WordPress 的 Eloquent ORM
Requires
- illuminate/database: ^8.0|^9.0|^10.0
- illuminate/pagination: ^8.0|^9.0|^10.0
README
感谢
Eloquent Wrapper for WordPress
这是一个库包,用于在 WordPress 中使用 Laravel 的 Eloquent ORM。
工作原理
- Eloquent 主要用作查询构建器
- WPDB 用于执行 Eloquent 构建的查询
- 因此,我们可以使用类似
debug-bar
或query-monitor
的插件来获取 SQL 查询报告。 - 它不会创建任何额外的 MySQL 连接
最低要求
- PHP 7.0
- WordPress 3.6+
包安装
$ composer require wenprise/eloquent
使用示例
基本用法
$db = \Wenprise\Eloquent\Connection::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 \Wenprise\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() );
文章
use Wenprise\ORM\WP\Post; // All published posts $posts = Post::published()->get(); $posts = Post::status('publish')->get(); // A specific post $post = Post::find(31); echo $post->post_title;
页面
页面类似于自定义文章类型。您可以使用 Post::type('page')
或 Wenprise\ORM\WP\Page
类。
// Find a page by slug $page = Page::slug('about')->first(); // OR $page = Post::type('page')->slug('about')->first(); echo $page->post_title;
评论
use Wenprise\ORM\WP\Comment; // Get Comment with id 12345 $comment = Comment::find(12345); // Get related data $comment->post; $comment->author; $comment->meta
元数据(自定义字段)
您也可以从文章中检索元数据。
// Get a custom meta value (like 'link' or whatever) from a post (any type) $post = Post::find(31); echo $post->meta->link; // OR echo $post->fields->link; echo $post->link; // OR
要从用户创建或更新元数据表单,请使用 saveMeta()
或 saveField()
方法。它们像 Eloquent 的 save()
方法一样返回 bool
。
$post = Post::find(1); $post->saveMeta('username', 'jgrossi');
您也可以同时保存多个元数据
$post = Post::find(1); $post->saveMeta([ 'username' => 'jgrossi', 'url' => 'http://jgrossi.com', ]);
您还有 createMeta()
和 createField()
方法,它们的工作方式与 saveX()
方法类似,但它们仅用于创建,并返回创建的 PostMeta
实例,而不是 bool
。
$post = Post::find(1); $postMeta = $post->createMeta('foo', 'bar'); // instance of PostMeta class $trueOrFalse = $post->saveMeta('foo', 'baz'); // boolean
通过自定义字段(元数据)查询文章
有多种方法可以通过自定义字段(元数据)查询文章。只需在 Post
类(实际上是所有使用 HasMetaFields
特性的模型)下使用 hasMeta()
范围即可。
// Using just one custom field $post = Post::published()->hasMeta('username', 'jgrossi')->first(); // setting key and value $post = Post::published()->hasMeta('username'); // setting just the key
您还可以使用 hasMeta()
范围并传递一个数组作为参数
$post = Post::hasMeta(['username' => 'jgrossi'])->first(); $post = Post::hasMeta(['username' => 'jgrossi', 'url' => 'jgrossi.com'])->first(); // Or just passing the keys $post = Post::hasMeta(['username', 'url'])->first();
字段别名
Post
类支持“别名”,因此如果您检查 Post
类,您应该注意静态 $aliases
数组中定义的一些别名,例如 title
为 post_title
和 content
为 post_content
。
$post = Post::find(1); $post->title === $post->post_title; // true
如果您正在扩展 Post
类以创建自己的类,您也可以使用 $aliases
。只需在您的类内部添加新别名到该静态属性即可,它将自动继承父 Post
类的所有别名
class A extends Post { protected static $aliases = [ 'foo' => 'post_foo', ]; } $a = A::find(1); echo $a->foo; echo $a->title; // from Post class
自定义范围
要排序文章,您可以使用 newest()
和 oldest()
范围,适用于 Post
和 User
类
$newest = Post::newest()->first(); $oldest = Post::oldest()->first();
分页
要排序文章,只需使用 Eloquent 的 paginate()
方法
$posts = Post::published()->paginate(5); foreach ($posts as $post) { // ... }
要显示分页链接,只需调用 links()
方法
{{ $posts->links() }}
文章分类
您可以通过以下方式获取特定文章的分类
$post = Post::find(1); $taxonomy = $post->taxonomies()->first(); echo $taxonomy->taxonomy;
或者,您可以使用其分类搜索文章
$post = Post::taxonomy('category', 'php')->first();
分类和分类法
获取一个分类或分类法或从特定分类加载文章。有多种方法可以实现这一点。
// all categories $cat = Taxonomy::category()->slug('uncategorized')->first()->posts(); echo "<pre>"; print_r($cat->name); echo "</pre>"; // only all categories and posts connected with it $cat = Taxonomy::where('taxonomy', 'category')->with('posts')->get(); $cat->each(function($category) { echo $category->name; });
附件和修订版
从 Post
或 Page
获取附件和/或修订版。
$page = Page::slug('about')->with('attachment')->first(); // get feature image from page or post print_r($page->attachment); $post = Post::slug('test')->with('revision')->first(); // get all revisions from a post or page print_r($post->revision);
用户
您可以使用与处理文章相同的方式处理用户。
// All users $users = User::get(); // A specific user $user = User::find(1); echo $user->user_login;
选项
您可以使用 Option
类从 wp_options
表获取数据
$siteUrl = Option::get('siteurl');
您也可以添加新选项
Option::add('foo', 'bar'); // stored as string Option::add('baz', ['one' => 'two']); // this will be serialized and saved
您可以以简单的数组形式获取所有选项
$options = Option::asArray(); echo $options['siteurl'];
或者,您可以指定只想获取的键
$options = Option::asArray(['siteurl', 'home', 'blogname']); echo $options['home'];
编写模型
use \Wenprise\Eloquent\Model; class Employee extends Model { /** * Name for table without prefix, the model can automatic add it * * @var string */ protected $table = 'table_name'; /** * Columns that can be edited * * @var array */ protected $fillable = []; /** * Disable created_at and update_at columns, unless you have those. */ public $timestamps = false; /** * 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' ]; /** * The column names allow to be filled * @var array */ protected $fillable = [ 'name', 'status', ]; } var_dump( Employee::all()->toArray() ); // gets all employees var_dump( Employee::find(1) ); // find employee with ID 1
类名 Employee
将会被转换成 PREFIX_employees
表以便进行查询。但是,按照惯例,您可以覆盖表名。