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

Performing Actions on Records at Read From Model

I seem to remember seeing a way (something like onRead()) to perform certain actions on records when they are read out of the database using the model, but I can't find it now.

For instance, I have several tables with first_name and last_name fields. When the records are read out of the database I'd like to add $record->name that is those two values combined with a space in between.

I'd also like date fields read from the database to have a corresponding unixtime attribute ($record->created_at_unixtime).

I think it would behave similar to beforeValidationOnCreate(), but happen when records are read out of the database.

Does that exist?



7.7k
Accepted
answer
edited May '14

I just stumbled across it while looking up something else! I couldn't remember the fetching name (pun intended).

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

It also has a corresponding function before something goes back into the database:

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

https://docs.phalcon.io/en/latest/reference/models.html