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

Transformation data

Are there some way to transform a model property value before encode the model to JSON ?

For example, i have the RatePlans with two properties created_at and updated_at. In the database those fields are datetime (Y-m-d H:i:s).

But, i need return those fields with ISO 8601 format. So i tried with a getter, getCreatedAt() for example, but doen't work.

How i can do this ?

Have you tried with this:

Initializing/Preparing fetched records

May be the case that after obtaining a record from the database is necessary to initialise the data before being used by the rest of the application. You can implement the method ‘afterFetch’ in a model, this event will be executed just after create the instance and assign the data to it:

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

https://docs.phalcon.io/en/latest/reference/models.html#initializing-preparing-fetched-records

Have you tried with this:

Initializing/Preparing fetched records

May be the case that after obtaining a record from the database is necessary to initialise the data before being used by the rest of the application. You can implement the method ‘afterFetch’ in a model, this event will be executed just after create the instance and assign the data to it:

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

https://docs.phalcon.io/en/latest/reference/models.html#initializing-preparing-fetched-records

Thanks for reply, but afterFetch only works on find method :(