Skip to content
dimitriBouteillePublic

About

✨ WordPress ORM with Eloquent, an object-relational mapper that makes it enjoyable to interact with your database.

Topics

Resources

Contributing

Stars

134 stars

Watchers

3 watching

Forks

Repository files navigation

WordPress ORM with Eloquent

GitHub Release Tests Packagist Downloads Eloquent version Coverage Status

WordPress ORM with Eloquent is a small library that adds a basic ORM into WordPress, which is easily extendable and includes models for core WordPress models such as posts, post metas, users, comments and more. The ORM is based on Eloquent ORM and uses the WordPress connection (wpdb class).

📖 Documentation: dimitribouteille.github.io/wp-orm

Tip

To simplify the integration of this library, we recommend using WordPress with one of the following tools: Bedrock, Themosis or Wordplate.

At a glance

use Dbout\WpOrm\Enums\PostStatus;
use Dbout\WpOrm\Models\CustomPost;

// A model for your custom post type
class Product extends CustomPost
{
    protected string $_type = 'product';

    protected function metaCasts(): array
    {
        return ['price' => 'float'];
    }
}

// Query it with Eloquent, filter on metas, eager load the relations
$products = Product::query()
    ->whereStatus(PostStatus::Publish)
    ->addMetaToFilter('featured', 'yes')
    ->with('author')
    ->orderBy(Product::DATE, 'desc')
    ->limit(10)
    ->get();

foreach ($products as $product) {
    echo $product->getPostTitle();          // post_title column
    echo $product->getMetaValue('price');   // float, thanks to the meta cast
    echo $product->author->getDisplayName();
}

// Create or update, without SQL
$product = Product::find(42);
$product->setPostTitle('Blue T-shirt');
$product->save();
$product->setMeta('price', 19.90);

No configuration is needed: the library uses the WordPress database connection.

Features

  • ✅ Support core WordPress models: Comment, Option, Post, Term, TermTaxonomy, TermRelationship, User, PostMeta and UserMeta
  • ✅ Support core WordPress post types: Article, Attachment and Page
  • ✅ Based on core WordPress database connection (wpdb class), no configuration required
  • ✅ Custom functions to filter models with meta
  • ✅ Meta casting (e.g. Attribute Casting)
  • ✅ Database migrations with WP-CLI (wp orm migrate)
  • ❤️ Easy integration of a custom post and comment type
  • ❤️ Easy model creation for projects with custom tables
  • ❤️ All the features available in Eloquent are usable with this library

Not yet developed but planned in a future version:

  • 🗓️ Multisite support (network-shared tables and switch_blog() handling)

Installation

Requires PHP >= 8.3 and WordPress >= 6.3.

composer require dbout/wp-orm

Make sure the Composer autoloader is loaded (e.g. in your wp-config.php), and you are ready to go. See Installation for the details.

Documentation

The full documentation is available on dimitribouteille.github.io/wp-orm. It only covers the specific points of this library, everything else works like Eloquent: see the documentation of Eloquent.

Warning

Every model is mass-assignable by default ($guarded = []) and the models bypass the WordPress hooks and object cache. Read Security and Limitations before using the library with user input.

Testing

🐞 This project includes two types of tests, both running on PHPUnit 12:

  • Unit tests - Isolated tests without WordPress dependencies
  • WordPress tests - Integration tests with WordPress core (uses wp-phpunit/wp-phpunit)
# Unit tests
composer run test:unit

# WordPress tests (requires Docker)
./run-wp-tests.sh

# With coverage
./run-wp-tests.sh --coverage

See TESTING.md for detailed setup instructions and troubleshooting.

Contributing

💕 🦄 We encourage you to contribute to this repository, so everyone can benefit from new features, bug fixes, and any other improvements. Have a look at our contributing guidelines to find out how to raise a pull request.

About

✨ WordPress ORM with Eloquent, an object-relational mapper that makes it enjoyable to interact with your database.

Topics

Resources

Contributing

Stars

134 stars

Watchers

3 watching

Forks

Releases

Used by

Contributors

Languages