Skip to content

Commit

Permalink
after update event
Browse files Browse the repository at this point in the history
  • Loading branch information
mathsgod committed May 24, 2022
1 parent 50281cd commit 840df79
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/Event/AfterDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace R\DB\Event;

use R\DB\ModelInterface;

class AfterDelete
{
public $target;
public function __construct($target)
public ModelInterface $target;
public function __construct(ModelInterface $target)
{
$this->target = $target;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Event/AfterUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace R\DB\Event;

use R\DB\ModelInterface;

class AfterUpdate
{
public $target;
public function __construct($target)
public ModelInterface $target;
public $source;
public function __construct(ModelInterface $target, $source)
{
$this->target = $target;
$this->source = $source;
}
}
6 changes: 4 additions & 2 deletions src/Event/BeforeDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace R\DB\Event;

use R\DB\ModelInterface;

class BeforeDelete
{
public $target;
public function __construct($target)
public ModelInterface $target;
public function __construct(ModelInterface $target)
{
$this->target = $target;
}
Expand Down
34 changes: 20 additions & 14 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Laminas\Db\Sql\Select;
use Laminas\Db\Sql\Where;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\Hydrator\ObjectPropertyHydrator;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\VarExporter\Internal\Hydrator;

abstract class Model implements ModelInterface
{
Expand Down Expand Up @@ -214,25 +216,32 @@ static function __attribute(string $name = null)
function save()
{
$error = $this->getValidator()->validate($this);
if (count($error) !== 0) {
throw new Exception($error);
if ($error->count() !== 0) {
throw new Exception($error->get(0)->getMessage());
}

$dispatcher = self::GetSchema()->eventDispatcher();
$gateway = static::__table_gateway();

$key = static::_key();


$hydrator = new ObjectPropertyHydrator();
$source = $hydrator->extract($this);

if ($this->$key) { // update
$mode = "update";
self::GetSchema()->eventDispatcher()->dispatch(new Event\BeforeUpdate($this));
$dispatcher->dispatch(new Event\BeforeUpdate($this));
} else { // insert
$mode = "insert";
self::GetSchema()->eventDispatcher()->dispatch(new Event\BeforeInsert($this));
$dispatcher->dispatch(new Event\BeforeInsert($this));
}

// generate record
$records = [];
$generated = [];

foreach (get_object_vars($this) as $name => $value) {
foreach ($source as $name => $value) {
if ($name[0] == "_" || $name == $key)
continue;

Expand Down Expand Up @@ -294,17 +303,13 @@ function save()
}
}



if ($mode == "update") { // update
$gateway = static::__table_gateway();
$ret = $gateway->update($records, [$key => $this->$key]);
self::GetSchema()->eventDispatcher()->dispatch(new Event\AfterUpdate($this));
$dispatcher->dispatch(new Event\AfterUpdate($this, $source));
} else {
$gateway = static::__table_gateway();
$ret = $gateway->insert($records);
$this->$key = $gateway->getLastInsertValue(); //save the id
self::GetSchema()->eventDispatcher()->dispatch(new Event\AfterInsert($this));
$dispatcher->dispatch(new Event\AfterInsert($this));
}

// if (count($generated)) {
Expand Down Expand Up @@ -340,14 +345,15 @@ function _id()
function delete()
{
$key = static::_key();
$gateway = new TableGateway(self::_table()->name, static::GetSchema()->getDbAdatpter());
static::GetSchema()->eventDispatcher()->dispatch(new Event\BeforeDelete($this));
$gateway = static::__table_gateway();
$dispatcher = self::GetSchema()->eventDispatcher();
$dispatcher->dispatch(new Event\BeforeDelete($this));
if (is_array($key)) {
$result = $gateway->delete($this->_id());
} else {
$result = $gateway->delete([$key => $this->$key]);
}
static::GetSchema()->eventDispatcher()->dispatch(new Event\AfterDelete($this));
$dispatcher->dispatch(new Event\AfterDelete($this));
return $result;
}

Expand Down

0 comments on commit 840df79

Please sign in to comment.