Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



5.1k
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);
}

http://docs.phalconphp.com/en/latest/reference/models.html