pollen-solutions/wp-database

Pollen Solutions - Wordpress Database Component - Wordpress adapter for the Pollen Database Component.

v1.0.2 2021-10-01 00:00 UTC

This package is auto-updated.

Last update: 2024-03-29 04:20:01 UTC


README

Latest Stable Version MIT Licensed PHP Supported Versions

WordPress Database Component is a WordPress adapter for the Pollen Database Component.

Installation

composer require pollen-solutions/wp-database

Basic Usage

User

Standard (with formatted meta-attributes appends)

use Pollen\WpDatabase\Eloquent\User;

$users = User::on()->limit(10)->get();
try {
    $data = json_encode($users->toArray(), JSON_THROW_ON_ERROR);
} catch (\Throwable $e ) {
    $data = $e->getMessage();
}
echo $data;

Include all non formatted metadatas

use Pollen\WpDatabase\Eloquent\User;

$users = User::on()->with('metas')->limit(10)->get();
try {
    $data = json_encode($users->toArray(), JSON_THROW_ON_ERROR);
} catch (\Throwable $e ) {
    $data = $e->getMessage();
}
echo $data;

With formatted meta-attributes disabled

use Pollen\WpDatabase\Eloquent\User;

$users = User::on()->limit(10)->get();
try {
    $data = json_encode($users->makeHidden(User::metaAttributes())->toArray(), JSON_THROW_ON_ERROR);
} catch (\Throwable $e ) {
    $data = $e->getMessage();
}
echo $data;

With all related posts (not recommended)

Major resource consumer, bad practice ...

use Pollen\WpDatabase\Eloquent\User;

$users = User::on()->with('posts')->find(1);
try {
    $data = json_encode($users->toArray(), JSON_THROW_ON_ERROR);
} catch (\Throwable $e ) {
    $data = $e->getMessage();
}
echo $data;

Role contraints

Global scope
namespace App\Model;

use Pollen\WpDatabase\Eloquent\User;

class Administrator extends User
{
    public $userRoleScope = 'administrator';
}

$admins = Administrator::on()->limit(10)->get();
try {
    $data = json_encode($admins->toArray(), JSON_THROW_ON_ERROR);
} catch (\Throwable $e ) {
    $data = $e->getMessage();
}
echo $data;
Dynamic scope
use Pollen\WpDatabase\Eloquent\User;

$users = User::on()->role('administrator')->limit(10)->get();
try {
    $data = json_encode($users->toArray(), JSON_THROW_ON_ERROR);
} catch (\Throwable $e ) {
    $data = $e->getMessage();
}
echo $data;

Blog contraints

Global scope
use Pollen\WpDatabase\Eloquent\User;

User::setBlogScope(2);

$users = User::on()->role('administrator')->limit(10)->get();
try {
    $data = json_encode($users->toArray(), JSON_THROW_ON_ERROR);
} catch (\Throwable $e ) {
    $data = $e->getMessage();
}
echo $data;

User::resetBlogScope();
Dynamic scope
use Pollen\WpDatabase\Eloquent\User;

$users = User::on()->blog(2)->limit(10)->get();
try {
    $data = json_encode($users->toArray(), JSON_THROW_ON_ERROR);
} catch (\Throwable $e ) {
    $data = $e->getMessage();
}
echo $data;