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


use WpMVC\Database\Schema;

Schema::create('products', function (Blueprint $table) {
	$table->big_increments('id');
	$table->unsigned_big_integer('category_id');
	$table->string('title');
	$table->long_text('description')->nullable();
	$table->enum('visibility', ['publish', 'draft'])->default('publish');
	$table->timestamps();

	$table->foreign('category_id')
		  ->references('id')
		  ->on('categories')
		  ->on_delete('cascade');
});

Schema::alter('products', function (Blueprint $table) {
	$table->string('short_description')->after('title')->nullable();
	$table->drop_column('legacy_column');
	$table->drop_index('index_abc123');
});

Schema::drop_if_exists('products');
Schema::rename('old_products', 'products');

$sql = Schema::create('products', function (Blueprint $table) {
	$table->string('title');
}, true);

echo $sql;

Schema::create('products', function (Blueprint $table) {
	$table->big_increments('id');
	$table->unsigned_big_integer('category_id');
	$table->string('title');
	$table->string('sku')->nullable();
	$table->long_text('description')->nullable();
	$table->decimal('price', 10, 2)->default(0.00);
	$table->boolean('is_active')->default(true);
	$table->enum('status', ['publish', 'draft'])->default('publish');
	$table->timestamps();

	$table->index(['status']);
	$table->foreign('category_id')
		  ->references('id')
		  ->on('categories')
		  ->on_delete('cascade');
});

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::query()->insert([
	[
		'post_author' => wp_get_current_user()->ID,
		'post_title' => 'Test Post 1',
	],
	[
		'post_author' => wp_get_current_user()->ID,
		'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' => 'Updated Post',
]);

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

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

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

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

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

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

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

$users = User::query()
	->left_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')
		 ->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();

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

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

$posts = Post::query()->order_by('post_title', 'asc')->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 {
	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 {
	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 {
	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);
	},
	'user',
])->get();