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

Disabled MVC model events and afterFetch problem.

I have a problem, when I try to handle models with disabled events.

When I take the model from documentation:

<?php

class Robots extends Phalcon\Mvc\Model
{

    public $id;

    public $name;

    public $status;

    public function beforeSave()
    {
        //Convert the array into a string
        $this->status = join(',', $this->status);
    }

    public function afterFetch()
    {
        //Convert the string to an array
        $this->status = explode(',', $this->status);
    }
}

Now in my controller:

\Phalcon\Mvc\Model::setup(array('events' => false));
$robot = Robots::findFirstById(1);
$robot->name = 'Mecha';
$robot->save();
\Phalcon\Mvc\Model::setup(array('events' => true));

The problem now is, that the afterFetch method is called, but the beforeSave not, so the status field is saved as array and php sends a notice message.

My workaround is, to call manually the beforeSave method, but for complex data change processes this can become unmaneagable.

Have someone a good practicable solution?

Judging by what you've described, the afterFetch method shouldn't be called at all. Maybe this should be a bug report. Also, try with the latest 1.3.0 - there are tons of bug fixes in that branch.

Sometimes I have actions in an event method that I don't want to execute every time. To do this, I rely on flags I set in the object. Of course - this won't help with afterFetch...