the-under-scorer / wp-eloquent
WordPress 的 Eloquent ORM
1.5.2
2019-10-08 10:51 UTC
Requires
- illuminate/database: ^5.4
- illuminate/pagination: ^5.4
Requires (Dev)
- phpunit/phpunit: ^7
- vlucas/phpdotenv: ^3.3
README
WordPress 的 Eloquent 包装器
这是一个用于在 WordPress 中使用 Laravel 的 Eloquent ORM 的库。它是原始 https://github.com/tareq1988/wp-eloquent 库的一个分支,似乎已经被遗弃。
包安装
要安装此包,请运行
$ composer require the-under-scorer/wp-eloquent
使用示例
基本使用
$db = \UnderScorer\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 \UnderScorer\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 数据库的自定义表来创建模型
namespace whatever;
class CustomTableModel extends \UnderScorer\ORM\Eloquent\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();
}
}
从表中检索所有行
$users = $db->table('users')->get(); foreach ($users as $user) { var_dump($user->display_name); }
在这里 users
是不带前缀的表名。前缀将自动应用。
其他示例
编写模型
use \UnderScorer\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 UnderScorer\WP\Post; var_dump( Post::all() ); //returns only posts with WordPress post_type "post"
通过 post_status
和 post_type
过滤 Post
use UnderScorer\WP\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-bar
或query-monitor
这样的插件来获取 SQL 查询报告。 - 它不会创建任何额外的 MySQL 连接
最低要求
- PHP 5.6.4+
- WordPress 4.0+