Alan James Sun Nov 04 13:55:10 -0500 2012

Subject: After Find Callbacks

I want to automatically run a callback after I find data in a database. The callback would do something like take a date field and separate the month day and year into individual properties of the object.

I have looked at the callback page and not really found a way to do this after a find command.

How do I do this?


Mr. Carl Tue Nov 06 09:54:28 -0500 2012

Why do you need to break it part vs just using format date?

 1 // Example 1
 2 // In your code somewhere where you need year/month  
 3 $year = $user->created_on->format('Y');        // Use php activerecord datatime model
 4 $month = $user->created_on->format('m');    // Use php activerecord datatime model
 5 
 6 // Example 2
 7 class User extends ActiveRecord\Model
 8 {
 9     public function getYear()
10     {
11         return $this->created_on->format('Y');
12     }
13     // ETC
14 }
15 
16 // Example 3
17 class User extends ActiveRecord\Model
18 {
19     public $year = 0;
20 
21     // Use the call back/constructor. (NEED check if it's need record)
22     // Some method
23     //$this->year = $this->created_on->format('Y')
24     //
25 }
Alan James Tue Nov 06 16:54:40 -0500 2012

I like the third example, but I'm not completely understanding it. What do you mean by "Use the call back/constructor." While I know what a call back and a constructor are I don't know how to use them.

To answer your other question.
Why do you need to break it part vs just using format date?

Because I may want to form other dynamic data other than dates. For example, lets say I have several fields that combine to make a URL to the user's photo. I would want to combine these fields into a URL property within my model, then be able to reference that property whenever I load a user object. If I did this in my controller or other parts of my code I would be performing the same logic possibly 50 - 100 times more than I need. If/when my schema changes I would have to update it in 50-100 places.

(1-2/2)