course when you register on learnphalcon
The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.

Part of getting to grips with the MVC approach is understanding where to put intelligence in a system. Some business rules can be enforced by adding intelligence to the database layer itself. In order to ensure that good quality data is captured in a system, we generally apply rules at the point of data capture. The "Model" in an MVC system allows information to be captured and processed relating to entities in the system.

A person's age in a system is a good example of something you would not want to store in a database. The information becomes wrong once a year and would have to be updated. If there are many Member's in the system it would be necessary to give someone the task of updating all the Member's whose birthday it is today. It is much simpler to store the person's date of birth and have the system calculate their age.

While it might be possible to do this on the View this information really belongs on the Model as although it is calculated information it is an attribute which directly relates to the entity. 

In this step, we will make a slight modification to the Member class to allow it to return the Member's age as a getter. This information can then easily be called anywhere we have a Member object. This will allow for the Member's age to be included on the Search view with only a minor modification to the code.

Given the Member's date of birth is stored in the protected attribute dateOfBirth here is a PHP function which will calculate the age and return it.

public function getAge()
    $dob = new \DateTime($this->dateOfBirth);
    $today = new \DateTime();
    $interval = $today->diff($dob);
    return $interval->format("%y");

This function isn't very sophisticated in that it doesn't take any account of whether the person's birthday has passed yet in the current year but for the purposes of illustrating adding intelligence to our Member class it will do nicely.

The Member.php class which was auto-generated by the scaffold is to be found in app\models. Open your preferred editor and add the above function to the class. It doesn't really matter where you put it but below the getDateOfBirth() function probably makes sense.

Now open the view for the Search page which we finished up on in the last post. This file which is called search.phtml is found under /app/views/member.

Scroll down to the foreach loop which generates the main table associated with the view it looks like this:

Now if you want the view to also display a column for the Members age add the following line of code underneath the element which produces the dateOfBirth column

<td><?php echo $member->getAge() ?></td>