All Projects → tareq1988 → Wp Eloquent

tareq1988 / Wp Eloquent

Eloquent ORM for WordPress

Projects that are alternatives of or similar to Wp Eloquent

Goloquent
This repo no longer under maintenance, please go to https://github.com/si3nloong/sqlike
Stars: ✭ 16 (-96.65%)
Mutual labels:  orm, eloquent, database, mysql
Mikro Orm
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL and SQLite databases.
Stars: ✭ 3,874 (+710.46%)
Mutual labels:  orm, database, mysql
Sqlboiler
Generate a Go ORM tailored to your database schema.
Stars: ✭ 4,497 (+840.79%)
Mutual labels:  orm, database, mysql
Core
All of the required core code
Stars: ✭ 139 (-70.92%)
Mutual labels:  wordpress, orm, database
Simple Crud
PHP library to provide magic CRUD in MySQL/Sqlite databases with zero configuration
Stars: ✭ 190 (-60.25%)
Mutual labels:  orm, database, mysql
Db
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
Stars: ✭ 2,832 (+492.47%)
Mutual labels:  orm, database, mysql
Myblog
python写的博客,支持3种数据库,现在挂在evilbinary.org
Stars: ✭ 121 (-74.69%)
Mutual labels:  wordpress, database, mysql
Pomelo.entityframeworkcore.mysql
Entity Framework Core provider for MySQL and MariaDB built on top of MySqlConnector
Stars: ✭ 2,099 (+339.12%)
Mutual labels:  orm, database, mysql
Sequelizer
A GUI Desktop App for export sequelize models from database automatically.
Stars: ✭ 273 (-42.89%)
Mutual labels:  orm, database, mysql
Node Orm2
Object Relational Mapping
Stars: ✭ 3,063 (+540.79%)
Mutual labels:  orm, database, mysql
Architect
A set of tools which enhances ORMs written in Python with more features
Stars: ✭ 320 (-33.05%)
Mutual labels:  orm, database, mysql
Gnorm
A database-first code generator for any language
Stars: ✭ 415 (-13.18%)
Mutual labels:  orm, database, mysql
Nut
Advanced, Powerful and easy to use ORM for Qt
Stars: ✭ 181 (-62.13%)
Mutual labels:  orm, database, mysql
Granite
ORM Model with Adapters for mysql, pg, sqlite in the Crystal Language.
Stars: ✭ 238 (-50.21%)
Mutual labels:  orm, database, mysql
Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+362.55%)
Mutual labels:  orm, database, mysql
Db Seeder
A database seeder app for MySQL
Stars: ✭ 77 (-83.89%)
Mutual labels:  wordpress, database, mysql
Jooq
jOOQ is the best way to write SQL in Java
Stars: ✭ 4,695 (+882.22%)
Mutual labels:  orm, database, mysql
Sqlingo
💥 A lightweight DSL & ORM which helps you to write SQL in Go.
Stars: ✭ 142 (-70.29%)
Mutual labels:  orm, database, mysql
Grimoire
Database access layer for golang
Stars: ✭ 151 (-68.41%)
Mutual labels:  orm, database, mysql
Wetland
A Node.js ORM, mapping-based. Works with MySQL, PostgreSQL, SQLite and more.
Stars: ✭ 261 (-45.4%)
Mutual labels:  orm, database, mysql

Eloquent Wrapper for WordPress

This is a library package to use Laravel's Eloquent ORM with WordPress.

Package Installation

To install this package, edit your composer.json file:

{
    "require": {
        "tareq1988/wp-eloquent": "dev-master"
    }
}

Now run:

$ composer install

Usage Example

Basic Usage

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

Creating Models For Custom Tables

You can use custom tables of the WordPress databases to create models:

<?php
namespace Whatever;

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

    /**
     * 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();
    }

}

Retrieving All Rows From A Table

$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.

Other Examples

Writing a Model

use \WeDevs\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.

In-built Models for WordPress

  • Post
  • Comment
  • Post Meta
  • User
  • User Meta
use WeDevs\ORM\WP\Post as Post;

var_dump( Post::all() ); //returns only posts with WordPress post_type "post"

Filter Post by post_status and post_type

use WeDevs\ORM\WP\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

How it Works

  • 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 or query-monitor to get SQL query reporting.
  • It doesn't create any extra MySQL connection

Minimum Requirement

  • PHP 5.6.4+
  • WordPress 4.0+

Author

Tareq Hasan

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].