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

afterFetch()

Hi, my database table for example "names": id int, name varchar, active int

I have something like this in model, yesterday or before yesterday it worked

class Names extends \Phalcon\Mvc\Model
{
    public function afterFetch()
    {
        if($this->id == 5)
        {
              $this->alert = "ALERT"; //alert isn't field in database
        }
    }
}

class IndexController extends \Phalcon\Mvc\Controller
{
   public function indexAction()
   {
       $names = Names::find()->toArrray();
       print_r($names);
   }
}

i got:

Array
(
    [id] => 4
    [name] => Android
    [active] => 1
)
Array
(
    [id] => 5
    [name] => Android
    [active] => 1
)

I need to get

Array
(
    [id] => 4
    [name] => Android
    [active] => 1
)
Array
(
    [id] => 5
    [name] => Android
    [active] => 1
    [alert] => Alert // <----- HERE is what i need to exist
)

only when i add field "alert" to database i can overwrite its value :(

Can you restore previous version?



98.9k

toArray() only exports the fields that are present in the models meta-data, there is no previous version that takes into account arbitrary fields added to the model.

Sorry, everything is ok :) I tested using toArray() if everything is ok because of simpliler view. I had bug in if/then which is more complicated than example. It is because i recompile some times a day :D and though that something changed Model method afterFetch() is amazing.

Please update documentation 1.2.0 about methods like afterFetch() with simple examples. Only on this forum is information about this, nothing else. Maybe Framework offers more new undocumented useful methods like this one. Now I use afterFetch() nearly with every model.