Skip to content

Conversation

@nawarian
Copy link

As described under this issue under Respect\Relational there's an implementation of a Proxy interface to add events onto Respect\Data.

This could be user as this:

$db = new \PDO('sqlite:test.db');
$aRelationalMapper = new Respect\Relational\Mapper($db);

$mapper = new Respect\Data\EventedMapper($aRelationalMapper);

// From now on we'll be using $mapper only

// First implementation, possibly a back-end one

// $eventName = "collectionName:eventName:suffix";
$mapper->on( 'person:insert:post', function($newPerson, $collection) {
    echo "New person created: [{$newPerson->id}] {$newPerson->name}";
} );

$person = new \stdClass();
$person->name = 'Nawarian';

$mapper->person->persist($person);
$mapper->flush(); // echoes "New person created: [1] Nawarian"

Same goes for every events: insert, update, delete. With suffixes: pre and post.
So there are six events to be listened:

  • insert:pre
  • insert:post
  • update:pre
  • update:post
  • delete:pre
  • delete:post

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this switch?

Can we use if instead?

    if (!(is_callable($action)) || !($action instanceof Listener)) {
        $message = 'Invalid type of action provided for event manager';
        throw new \InvalidArgumentException($message, 400);
    }

    // The block below you can use for control or remove it
    $events = $this->getEvents();
    if (array_key_exists("attach", $events)) {
        $this->dispatch("attach", array($eventName, $action));
    }

    $this->events[$eventName][] = $action;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably for future implementations or checks. I don't see any trouble by changing it.
@Rafael-BP could you?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, no problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants