https://github.com/pollen-solutions/wp-database
Pollen Solutions - Wordpress Database Component - Wordpress adapter for the Pollen Database Component
https://github.com/pollen-solutions/wp-database
php wordpress
Last synced: 2 months ago
JSON representation
Pollen Solutions - Wordpress Database Component - Wordpress adapter for the Pollen Database Component
- Host: GitHub
- URL: https://github.com/pollen-solutions/wp-database
- Owner: pollen-solutions
- License: mit
- Created: 2021-08-12T13:30:57.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-01T16:21:57.000Z (almost 5 years ago)
- Last Synced: 2025-01-14T11:58:17.817Z (over 1 year ago)
- Topics: php, wordpress
- Language: PHP
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# WordPress Database
[](https://packagist.org/packages/pollen-solutions/wp-database)
[](LICENSE.md)
[](https://www.php.net/supported-versions.php)
**WordPress Database** Component is a WordPress adapter for the Pollen Database Component.
## Installation
```bash
composer require pollen-solutions/wp-database
```
## Basic Usage
### User
#### Standard (with formatted meta-attributes appends)
```php
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
```php
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
```php
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 ...
```php
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
```php
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
```php
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
```php
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
```php
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;
```