[SOLVED] Recording execution time to a database

I’d like to record the page execution to a database - I’ve generated a model and the code do what I want, but I don’t know where or how to attach the code. It needs to be run as the last thing in the Yii:app process so it can attain the logger’s execution time.

I have a model called VisitorLogModel, and created a new function within it that does the following:


    

public function LogVisit()

{

        if (!Yii::app()->params['debug']):

            $mdl_VisitorLog             = new VisitorLogModel();

            $mdl_VisitorLog->page_url   = Yii::app()->getRequest()->url;

            $mdl_VisitorLog->referrer_url = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : null;

            $mdl_VisitorLog->yii_version= Yii::getVersion();

            $mdl_VisitorLog->execution_time = (string)Yii::getLogger()->getExecutionTime();

            $mdl_VisitorLog->insert();

            unset($mdl_VisitorLog);

        endif;

}



Where should I run this function? Where do I attach this behaviour/function call?

I do have a Controller called MasterController (which extends CController), which all my Controllers extend - so MasterController is run on every page load - would it be something I can put in here?

Thank you in advance!


Thanks to Troto for mentioning ‘onEndRequest’!

Has no one tried this before? It would seem like a basic thing to do? All I want to do is at the end of a page run record the execution time of the page - is there any way to do this?

Create a function (or a public static method in a class) that logs the execution time.

in your config file add another entry like:




'onEndRequest'=>'functionName',



or




'onEndRequest'=>array('ClassName','methodName'),



Thank you Troto, that’s perfect! Got it solved :D