1. Go to this page and download the library: Download mjkhajeh/wporm 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/ */
mjkhajeh / wporm example snippets
use MJ\WPORM\Model;
use MJ\WPORM\Blueprint;
class Parts extends Model {
protected $table = 'parts';
protected $fillable = ['id', 'part_id', 'qty', 'product_id'];
protected $timestamps = false;
public function up(Blueprint $blueprint) {
$blueprint->id();
$blueprint->integer('part_id');
$blueprint->integer('product_id');
$blueprint->integer('qty');
$blueprint->index('product_id');
$this->schema = $blueprint->toSql();
}
}
use MJ\WPORM\SchemaBuilder;
$schema = new SchemaBuilder($wpdb);
$schema->create('parts', function($table) {
$table->id();
$table->integer('part_id');
$table->integer('product_id');
$table->integer('qty');
$table->index('product_id');
});
// Get all parts
$all = Parts::all();
// Find by primary key
$part = Parts::find(1);
// Where clause
$parts = Parts::query()->where('qty', '>', 5)->orderBy('qty', 'desc')->limit(10)->get(); // Limit to 10 results
// First result
$first = Parts::query()->where('product_id', 2)->first();
// Update if a user with this email exists, otherwise create a new one
$user = User::updateOrCreate(
['email' => '[email protected]'],
['name' => 'John Doe', 'country' => 'US']
);
// Disable global scopes for this call
$user = User::updateOrCreate(
['email' => '[email protected]'],
['name' => 'John Doe', 'country' => 'US'],
false // disables global scopes
);
// Get the first user with this email, or create if not found
$user = User::firstOrCreate(
['email' => '[email protected]'],
['name' => 'Jane Doe', 'country' => 'US']
);
// Disable global scopes for this call
$user = User::firstOrCreate(
['email' => '[email protected]'],
['name' => 'Jane Doe', 'country' => 'US'],
false // disables global scopes
);
// Get the first user with this email, or instantiate (but do not save) if not found
$user = User::firstOrNew(
['email' => '[email protected]'],
['name' => 'Jane Doe', 'country' => 'US']
);
// Disable global scopes for this call
$user = User::firstOrNew(
['email' => '[email protected]'],
['name' => 'Jane Doe', 'country' => 'US'],
false // disables global scopes
);
if (!$user->exists) {
$user->save(); // Save if you want to persist
}
public function profile() {
return $this->hasOne(Profile::class, 'user_id');
}
public function posts() {
return $this->hasMany(Post::class, 'user_id');
}
public function user() {
return $this->belongsTo(User::class, 'user_id');
}
public function roles() {
return $this->belongsToMany(Role::class, 'user_role', 'user_id', 'role_id');
}
public function comments() {
return $this->hasManyThrough(Comment::class, Post::class, 'user_id', 'post_id');
}
// Users with at least one post
User::query()->has('posts')->get();
// Users with at least 5 posts
User::query()->has('posts', '>=', 5)->get();
// Users with exactly 2 posts
User::query()->has('posts', '=', 2)->get();
// Users with at least one published post
User::query()->whereHas('posts', function($q) {
$q->where('published', 1);
})->get();
public function getQtyAttribute() {
return $this->attributes['qty'] * 2;
}
public function setQtyAttribute($value) {
$this->attributes['qty'] = $value / 2;
}
protected $appends = ['user'];
public function getUserAttribute() {
return get_user_by('id', $this->user_id);
}
Parts::query()->beginTransaction();
// ...
Parts::query()->commit();
// or
Parts::query()->rollBack();
// Using the query builder for a custom select
$results = Parts::query()
->select(['part_id', 'SUM(qty) as total_qty'])
->where('product_id', 2)
->orderBy('total_qty', 'desc')
->limit(5) // Limit to top 5 parts
->get();
// Using $wpdb directly for full custom SQL
global $wpdb;
$table = (new Parts)->getTable();
$results = $wpdb->get_results(
$wpdb->prepare("SELECT part_id, SUM(qty) as total_qty FROM $table WHERE product_id = %d GROUP BY part_id", 2),
ARRAY_A
);
class Parts extends Model {
// ...existing code...
public static function partsWithMinQty($minQty) {
return static::query()->where('qty', '>=', $minQty)->get();
}
}
// Usage:
$parts = Parts::partsWithMinQty(5);
use MJ\WPORM\DB;
// Update posts with IDs 3, 4, 5
db::table('post')
->whereIn('id', [3, 4, 5])
->update(['title' => 'Updated Title']);
// Select rows from any table
db::table('custom_table')->where('status', 'active')->get();
global $wpdb;
$table = (new User)->getTable();
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM $table WHERE (country = %s AND (age >= %d OR verified = %d)) OR (country = %s AND subscribed = %d)",
'US', 18, 1, 'CA', 1
),
ARRAY_A
);
// Start a new query chain for the User model
$query = User::newQuery();
$activeUsers = $query->where('active', true)->get();
use MJ\WPORM\Model;
use MJ\WPORM\Blueprint;
class Article extends Model {
protected $table = 'articles';
protected $fillable = ['id', 'title', 'content', 'created_on', 'changed_on'];
protected $timestamps = true; // default is true
protected $createdAtColumn = 'created_on';
protected $updatedAtColumn = 'changed_on';
public function up(Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamp('created_on');
$table->timestamp('changed_on');
$this->schema = $table->toSql();
}
}
use MJ\WPORM\Model;
use MJ\WPORM\Blueprint;
class LogEntry extends Model {
protected $table = 'log_entries';
protected $fillable = ['id', 'message'];
protected $timestamps = false;
public function up(Blueprint $table) {
$table->id();
$table->string('message');
$this->schema = $table->toSql();
}
}
class Post extends \MJ\WPORM\Model {
protected static function boot() {
parent::boot();
static::addGlobalScope('published', function($query) {
$query->where('status', 'published');
});
}
}
$posts = Post::all(); // Only published posts
$allPosts = Post::query(false)->get(); // disables all global scopes
// or
$allPosts = Post::query()->withoutGlobalScopes()->get();
Post::removeGlobalScope('published');
class User extends Model {
protected $softDeletes = true;
// Optionally customize the deleted_at column:
// protected $deletedAtColumn = 'deleted_at';
// Optionally set the soft delete type (see below)
// protected $softDeleteType = 'timestamp'; // or 'boolean'
}
class User extends Model {
protected $softDeletes = true;
// protected $deletedAtColumn = 'deleted_at'; // optional
// protected $softDeleteType = 'timestamp'; // optional, default
}
// For large datasets, use limit and offset for pagination:
$usersPage2 = User::query()->orderBy('id')->limit(20)->offset(20)->get(); // Get users 21-40
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.