Laravel Eloquent based ORM for WordPress
This is a fork of the original library written by Tareq Hasan with some more improvements and changes.
To install the library run:
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() );
You can use custom tables of the WordPress databases to create models:
<?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);
}
Here users
is the table name without prefix. The prefix will be applied automatically.
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
The class name Employee
will be translated into PREFIX_employees
table to run queries. But as usual, you can override the table name.
- Post
- Comment
- Post Meta
- User
- User Meta
use IgniteKit\WP\ORM\Models\Post as Post;
var_dump( Post::all() ); //returns only posts with WordPress post_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 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-bar
orquery-monitor
to get SQL query reporting. - It doesn't create any extra MySQL connection
- 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://www.gnu.org/licenses/>.