4
In many cases we have data in the database that is not in the format we want them to be shown.
As an example, let’s say I have a table called users and in this table a field called role which will store a user’s access level. Let’s say the value of role be it ADMIN.
When I plan to output this data after retrieving it from the database, I don’t want the user to see the value of the field role (which in this case is ADMIN) but I want him to see DIRECTOR. This is a fictitious example to illustrate that actually there are many situations where in the database the stored data is one but the output of it to the user must be another.
My question is: how do you usually treat it at the level of PHP?
Using the framework Cakephp i am currently using the callback afterFind which takes the current result obtained in the database and creates an additional element in the array with the index the and there I treat all the output data.
An example. Suppose I got the following array of results from the database:
user_id => 1,
username => 'joao',
role => 'ADMIN'
After passing through my callback, the array will be:
user_id => 1,
username => 'joao',
role => 'ADMIN'
o =>
user_id => 1,
username => 'joao',
role => 'DIRETOR'
And at the time of output, that is to print in the view, I only use the index the. The rest are for internal use in controllers.