If you are a plugin writer like me and you write random WordPress plugins for various purposes, chances are you have to write many SQL queries for many cases. But if you look into modern PHP frameworks like Laravel, Symfony, Zend and every other frameworks, you will see they don’t write much raw SQL queries by hand. Instead, everyone is using ORM. In few cases you might have to write raw SQL queries, with ORM you can do that as well.
If you look at the WordPress world, we are still stuck with the WPDB global object. It’s an Active Record class, but can’t be called an ORM. The main reason I like ORM is it reduces so much codes and facilitates many magical features without writing raw queries, instead of manipulate them using an Object Oriented like language. Also you can use many adapters for many databases (though it’s not possible right now in WordPress).
If you are writing a large plugin which involves many custom database tables, chances are you will get lost among those tables and queries very shortly. It happened to me as well and I was looking for using ORM’s in WordPress. But there aren’t so many good tools for that. So I looked into Laravel as it’s huge popular now a days. Laravel uses a package named Eloquent as its ORM provider. As it’s a composer package, you can easily use Eloquent into any PHP application.
There are ways to use Eloquent independently, but it requires you to create a separate connection to database. But as WordPress is already connected to the database, why create a new connection? And if we use Eloquent like that, we will miss database reporting from various debugging plugins like debug-bar, query-monitor, etc.
That’s why I wrote a composer package to use Eloquent ORM inside WordPress using the same database connection using WPDB. The benefits are:
- Eloquent is mainly used here as the query builder
- WPDB is used to run queries built by Eloquent
- It doesn’t create any extra MySQL connection
- You get to use the WordPress debug tools/plugins
The package is not perfect, but please do checkout the Github repository for detailed information and usage instruction. Pull requests are always welcome.
Long awaited orm for wp 😀 thanks vaia
Hi, how to load your package in a wordpress existing ? At the root path, themes ? And how ?
Thank you !!
You can use the package in your plugin or theme. Check the github link and it has the package installation block. You have to use composer to install the package.
Yep, i have well the /vendor directory in my directory themes, but how to load or where to load files ?
Composer gives you autoloading functionality. So all you have to include is
require __DIR__ . '/vendor/autoload.php';
.Ok, i insert this line in my functions.php ?
composer.json is in the root path of wordpress
vendor is in the root
and i had : require_once(__DIR__ . ‘/vendor/autoload.php’);
BUT BLANK PAGE ??
I call well the autoload, but BLANK PAGE, any help ?!
That’s work !!
I’ve got problem with my composer install…
Needed this big time today working on converting a Laravel application into a WordPress plugin – thanks man!
Ah shoot, thought it would be okay with PHP 5.3 but the dependencies need 5.4 which alienates ~40% of users if I use it. Thanks anyways 🙁
Hi! Wanted to ask you what’s the implementation of the “many to many” relationship. I’ve been trying to use the $this->belongsToMany Eloquent’s way but it’s not working
belongsToMany works, here’s an example we are using on WP ERP.
biutiful! Thanks for the wrapper and the answer, Tareq!