PHP code example of wpmvc / database

1. Go to this page and download the library: Download wpmvc/database 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/ */

    

wpmvc / database example snippets




namespace WpMVC\App\Models;

use WpMVC\Database\Eloquent\Model;
use WpMVC\Database\Resolver;

class Post extends Model {

	public static function get_table_name():string {
		return 'posts';
	}

	public function resolver():Resolver {
		return new Resolver;
	}
}

Post::query()->insert([
	'post_author' => wp_get_current_user()->ID,
	'post_title' => "Test Post"
	...
]);
		

$post_author = wp_get_current_user()->ID;

Post::query()->insert([
	[
		'post_author' => $post_author,
		'post_title' => "Test Post 1"
		...
	],
	[
		'post_author' => $post_author,
		'post_title' => "Test Post 2"
		...
	]
]);

$post_id = Post::query()->insert_get_id([
	'post_author' => wp_get_current_user()->ID,
	'post_title' => "Test Post"
	// ...
]);

Post::query()->where('post_id', 100)->update([
	'post_title' => "Test Post"
]);

Post::query()->where('post_id', 100)->delete();

$posts = Post::query()->count();

$posts = Post::query()->get();

$posts = Post::query()->where('id', 100)->first();

$posts = Post::query()->select('post_title', 'post_date')->get();


$posts = Post::query()->distinct()->select('post_title', 'post_date')->get();

$users = User::query()
                ->join('contacts', 'users.id', '=', 'contacts.user_id')
                ->select('users.*', 'contacts.phone', 'contacts.email')
                ->get();

$users = User::query()
                ->join('contacts', 'users.id', '=', 'contacts.user_id')
                ->join('orders', 'users.id', '=', 'orders.user_id')
                ->select('users.*', 'contacts.phone', 'orders.price')
                ->get();


$users = User::query()
            ->left_join('posts', 'users.id', '=', 'posts.user_id')
            ->get();

$users = User::query()
            ->right_join('posts', 'users.id', '=', 'posts.user_id')
            ->get();


use WpMVC\Database\Query\JoinClause;

$posts = Post::query()->join('postmeta', function (JoinClause $join) {
	$join->on('postmeta.post_id', '=', 'posts.ID')->orOn(/* ... */);
})->get();

$posts = Post::query()->join('postmeta', function (JoinClause $join) {
	$join->on('postmeta.post_id', '=', 'posts.ID')->where('postmeta.meta_value', '>', 500);
})->get();

$posts = Post::query()->where('post_status', 'publish')->get();

$posts = Post::query()->where('post_status', 'publish')->orWhere('post_title', 'test post')->get();

	$posts = Post::query()->where_exists(function(Builder $query) {
		$query->select(1)->from('postmeta')->where_column('postmeta.post_id', 'posts.id')->limit(1);
	})->get();
	

	$post_meta = PostMeta::query()->select(1)->where_column('postmeta.post_id', 'posts.id')->limit(1);
	$posts     = Post::query()->where_exists($post_meta)->get();
	

$posts = Post::query()->where_between('ID', [1, 100])->get();

$posts = Post::query()->where_not_between('ID', [1, 100])->get();

$posts = Post::query()->where_in('ID', [100, 105])->get();

$posts = Post::query()->where_not_in('ID', [100, 105])->get();

$posts = Post::query()->order_by('post_title')->get();

$posts = Post::query()->order_by('post_title')->order_by_desc('post_status')->get();

$posts = Post::query()->group_by('post_author')->having('post_author', '>', 100)->get();


$posts = Post::query()->offset(10)->limit(5)->get();



namespace WpMVC\App\Models;

use WpMVC\Database\Eloquent\Model;
use WpMVC\Database\Eloquent\Relations\HasOne;

class User extends Model {

	/**
     * Get the phone associated with the user.
     */
    public function phone(): HasOne
    {
        return $this->has_one(Phone::class, 'ID', 'user_id');
    }
}

$users = User::query()->with('phone')->get();



namespace WpMVC\App\Models;

use WpMVC\Database\Eloquent\Model;
use WpMVC\Database\Eloquent\Relations\HasMany;

class Post extends Model {

	/**
     * Get the all meta associated with the user.
     */
    public function meta(): HasMany
    {
        return $this->has_many(PostMeta::class, 'ID', 'post_id');
    }
}



namespace WpMVC\App\Models;

use WpMVC\Database\Eloquent\Model;
use WpMVC\Database\Eloquent\Relations\BelongsToOne;

class PostMeta extends Model {

	/**
     * Get the post that owns the meta.
     */
    public function post(): BelongsToOne
    {
        return $this->belongs_to_one(Post::class, 'post_id', 'ID');
    }
}

use WpMVC\Database\Query\Builder;

$posts = Post::query()->with('meta', function(Builder $query) {
	$query->where('meta_id', 672);
})->get();

$posts = Post::query()->with([
			'meta' => function (Builder $query) {
				$query->where('meta_id', 672);
			},
			'user'
		])->get();