We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to get all event types (do Phalcon has onLoad event)

I am trying to get all the event types using this code:

class News extends Phalcon\Mvc\Model
{

    public function initialize()
    {
            $eventsManager = new \Phalcon\Events\Manager();

            $eventsManager->attach('model', function($event, $robot) {
                print_r($event->getType());
                return true;
            });
    }
...

but it never gets triggered (on save, view, etc.). Am I doing something wrong?

In fact what I want is to have "on load" event where to change the properties of the model on every loaded record from db (for example if I have a field with json and want to convert it to an array). Is it possible?



32.5k

How do you think, what way your model should know that it have to trigger event in this manager? In fact this code just create some events manager and add some function to it. That's all...

Look on the other examples of events manager, it alway has been passed to a target object which calls events: ```php $dispatcher->setEventsManager($eventsManager); $db->setEventsManager($eventsManager); // ect.


Any model is already the event listener for model events. Other words, It's the event manager of itself. You just have to create respective function and it will be triggered: ```php
class News extends Phalcon\Mvc\Model
{
    public function beforeValidation()
    {

    }

All built-in models events are represented in the docs https://docs.phalcon.io/en/latest/reference/models.html#events-and-events-manager

"onLoad event" could be done without events. You may feel free to override any model method:

class News extends Phalcon\Mvc\Model
{
    public $id;
    public $data;

    public function find($parameters)
    {
        $result = parent::find($parameters);
        // do what you want with result
        return $result;
    }

    public function findFirstById($parameters)
    {
        $result = parent::findFirstById($parameters);
        // do what you want with result
        return $result;
    }


7.9k

I have red the documentation before writing my question. The code I posted above is from there but it doesn't seem to work. There is no onLoad event in the standard events so my question is if it is possible to create it because I cannot figure out how to do it. Thanks.



32.5k

Yeah, seems there is a typo in docs. I'm sure there is should be some php $this->setEventsManager($eventsManager); at the bottom of initialize in your example.

If you say that onLoad is the event occurs on reading any record from db, then overriding find/findFirst methods is exactly what you need. Because any reading in a model is passed through these methods. Anyway if you want to convert readed data you will have to write specific code for every model (every different table structure), so override a method isn't much different from creating event handler.



7.9k

Unfortunately it's not that easy :) If I do it that way I have to overwrite the find method which returns multiple results (or more precisely an iterator). Even if it is possible it doesn't feels the right way to do it. I can also make a model's method and to pass the record through it before using it but doesn't feels the right way either.

I think that Eavu has got right. But please write your case, if needed change your data. Give us your table and what you want to achieve by Event. Overwrite find, findFirst etc should solve your json problem, or create functin based on find,findFirst

  public function findJson($parameters)
  {
    $result = parent::find($parameters);
    // do what you want with result
    return $result;
  }


98.9k

'onConstruct' is now available in 1.2.0:

class MyModel extends Phalcon\Mvc\Model
{
        public function onConstruct()
        {
            //do initialization stuff            
        }
}

and 'afterFetch':

class MyModel extends Phalcon\Mvc\Model
{
        public function afterFetch()
        {
            //do initialization stuff after findFirst/find          
        }
}


7.9k

Thank you very much! This is what I nedded :)

This is true amazing :)