PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] Passing a message from a listener

 February 22, 2022     cakephp, cakephp-3.0     No comments   

Issue

I have an event that I'm creating in my model's beforeSave function:

  public function beforeSave(Event $event, Entity $entity) {
    $event = new Event('Model.Transaction.createTransaction', $this, [
      'transaction' => $entity
    ]);
    $this->eventManager()->dispatch($event);

    if ($event->isStopped()) {
      return FALSE;
    }
  }

Which is grabbed by a Listener I have created:

public function implementedEvents() {
  return [
    'Model.Transaction.createTransaction' => 'createHistorical'
  ];
}

This all works fine. What I was wondering is if there's any way to pass a message back to the model where the event was registered. So that if there's a failure or something, I can pass a message back to the model and add it to the errors to be displayed on the form?


Solution

You can return a value from the callback method (createHistorical in your case) which would be accessible using $event->result after dispatching is done.



Answered By - ADmad
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing