ignitekit/wp-orm

WordPress 的 Laravel Eloquent 激发 ORM 包

1.2.0 2022-07-03 20:13 UTC

This package is auto-updated.

Last update: 2024-09-22 03:09:21 UTC


README

基于 Laravel Eloquent 的 WordPress ORM

这是基于 Tareq Hasan 编写的原始库的分支,增加了一些改进和更改。

包安装

要安装库,运行

composer require ignitekit/wp-orm

使用示例

基本使用

$db = \IgniteKit\WP\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 \IgniteKit\WP\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() );

为自定义表创建模型

您可以使用 WordPress 数据库的自定义表来创建模型

<?php
namespace Whatever;

use IgniteKit\WP\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' ];

}

从表中检索所有行

$users = $db->table('users')->get();

foreach ($users as $user) {
    var_dump($user->display_name);
}

这里 users 是表名 不包含前缀。前缀将自动应用。

其他示例

编写模型

use \IgniteKit\WP\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

类名 Employee 将被转换为 PREFIX_employees 表以运行查询。但通常,您可以覆盖表名。

WordPress 内置模型

  • 文章
  • 评论
  • 文章元数据
  • 用户
  • 用户元数据
use IgniteKit\WP\ORM\Models\Post as Post;

var_dump( Post::all() ); //returns only posts with WordPress post_type "post"
通过 post_statuspost_type 过滤 Post
use IgniteKit\WP\ORM\Models\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

工作原理

  • Eloquent 主要用作查询构建器
  • 使用 WPDB 来运行 Eloquent 构建的查询
  • 因此,我们可以使用像 debug-barquery-monitor 这样的插件来获取 SQL 查询报告。
  • 它不会创建任何额外的 MySQL 连接

最低要求

  • PHP 5.6.4+
  • WordPress 4.0+

许可

Copyright (C) 2020 Darko Gjorgjijoski (https://darkog.com)

This file is part of wp-orm

WP ORM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

WP ORM  is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with WP ORM. If not, see <https://gnu.ac.cn/licenses/>.