PHP code example of hollisho / wp-orm

1. Go to this page and download the library: Download hollisho/wp-orm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hollisho / wp-orm example snippets


use WPOrm\Model\Model;

class Post extends Model
{
    protected static string $table = 'posts';
    
    public function author()
    {
        return $this->belongsTo(User::class, 'post_author');
    }
}

// Query
$posts = Post::where('post_status', 'publish')
    ->orderBy('post_date', 'desc')
    ->limit(10)
    ->get();

// Find by ID
$post = Post::find(1);

// Relationships
$author = $post->author;

use WPOrm\Database\ConnectionManager;

ConnectionManager::configure([
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => DB_HOST,
            'database' => DB_NAME,
            'username' => DB_USER,
            'password' => DB_PASSWORD,
            'charset' => DB_CHARSET,
            'prefix' => $GLOBALS['wpdb']->prefix,
        ]
    ]
]);

ConnectionManager::configure([
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'read' => [
                'host' => '192.168.1.2',
            ],
            'write' => [
                'host' => '192.168.1.1',
            ],
            'driver' => 'mysql',
            'database' => DB_NAME,
            'username' => DB_USER,
            'password' => DB_PASSWORD,
            'charset' => DB_CHARSET,
            'prefix' => $GLOBALS['wpdb']->prefix,
        ]
    ]
]);

// Automatically uses the correct table prefix for the current site
$posts = Post::onSite(2)->where('post_status', 'publish')->get();

// Or use global tables
$users = User::global()->get();