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
Requires
- php: ^7.4 || ^8.0
- pollen-solutions/database: ^1.0
- pollen-solutions/support: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- roave/security-advisories: dev-latest
Suggests
- pollen-solutions/container: Pollen Solutions - Container Component - PSR-11 ready Dependencies Injection Container.
This package is auto-updated.
Last update: 2024-11-29 06:22:56 UTC
README
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;