Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 3.3.0 #77

Merged
merged 12 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ jobs:

strategy:
matrix:
wp_version: ["6.3", "6.4", "6.5", "latest"]
wp_version: ["6.3", "6.4", "6.5", "6.6", "latest"]

services:
mysql:
image: mysql:8.4
image: mysql:9.0
env:
MYSQL_ALLOW_EMPTY_PASSWORD: false
ports:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This documentation only covers the specific points of this library, if you want
- [With findOneBy*](/doc/filter-data.md#with-findoneby)
- [With taps](/doc/filter-data.md#with-taps)
- [With query builder](/doc/filter-data.md#with-query-builder)
- [Events](/doc/events.md)
- [Create custom model](doc/create-model.md)
- [Generic Model](doc/create-model.md#generic-model)
- [Custom Post Type Model](doc/create-model.md#custom-post-type-model)
Expand Down
58 changes: 58 additions & 0 deletions doc/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Events

Eloquent models dispatch several events, allowing you to hook into the following moments in a model's lifecycle: `retrieved`, `creating`, `created`, `updating`, `updated`, `saving`, `saved`, `deleting`, `deleted`, `trashed`, `forceDeleting`, `forceDeleted`, `restoring`, `restored`, and `replicating`.

> 📘 The following few lines explain how to use events with the library, if you want to know more you can look at the documentation: [Eloquent - Events](https://laravel.com/docs/10.x/eloquent#events).

## How to use Eloquent events ?

By default, Eloquent events are not functional in the library since Eloquent is initially used with Laravel which contains all the logic to dispatch the events. However, it is possible to use events via the [illuminate/events](https://packagist.org/packages/illuminate/events) library.

1. First, install `illuminate/events` :

```bash
composer require illuminate/events ^10.0
```

2. Setup the dispatcher in your model with `setEventDispatcher` function :

```php
use Illuminate\Events\Dispatcher;
use Dbout\WpOrm\Orm\AbstractModel;

class User extends AbstractModel
{

protected static function boot()
{
static::setEventDispatcher(new Dispatcher());
parent::boot();
}
}
```

3. Create your events :)

```php
use Illuminate\Events\Dispatcher;
use Dbout\WpOrm\Orm\AbstractModel;

class User extends AbstractModel
{

protected static function boot()
{
static::setEventDispatcher(new Dispatcher());
parent::boot();

static::saved(function (User $user) {
// Add your logic here
});
}
}
```

You can now use events, you can use [$dispatchesEvents](https://laravel.com/docs/10.x/eloquent#events) property or [closures](https://laravel.com/docs/10.x/eloquent#events-using-closures).


> Warning, if you use abstract class, you must call `setEventDispatcher` in child class.
6 changes: 3 additions & 3 deletions src/Builders/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function setModel(Model $model)
protected function _whereOrIn(string $columns, array $value): self
{
$first = reset($value);
if(is_array($first)) {
if (is_array($first)) {
$this->whereIn($columns, $first);
} elseif(count($value) == 1) {
} elseif (count($value) == 1) {
$this->where($columns, reset($value));
} else {
$this->whereIn($columns, $value);
Expand All @@ -51,7 +51,7 @@ protected function _whereOrIn(string $columns, array $value): self
protected function joined($query, $table): bool
{
$joins = $query->getQuery()->joins;
if($joins == null) {
if ($joins == null) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Enums/PingStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
*/
enum PingStatus: string
{
case Closed = 'closed';
case Closed = 'closed';
case Open = 'open';
}
2 changes: 1 addition & 1 deletion src/Orm/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(array $attributes = [])
/**
* @inheritDoc
*/
public function getTable(): string
public function getTable()
{
$prefix = $this->getConnection()->getTablePrefix();
if ($this->table !== null && $this->table !== '') {
Expand Down