Question

I'm creating an audit trail module for a project using Yii php, and i'm quite a newbi.. and as we all know audit trail is concerned with the creation of history, transaction logs... i've created a model for the audit trail and generated a controller as well as a separate model for it. (Using gii of course)

My problem lies with how to actually create an entry , or a trail (log, history, i don't know the term sorry =3)

I have my AuditTrailController with an action:

class AuditTrailController extends Controller{

    public function actionCreate()
    {
    //do create the trail   
    }

}

My plan is to call the actionCreate() from every other controllers from all over the modules in the whole project like say:

class StudyController extends Controller {

        public function addRecord(){
        //add some record


        //---> i want to call the acionCreate() from the AuditTrailController from here
        //to create an entry telling that the some user "user" at time "time"
        //performed an "add" operation..
        }

        public function updateRecord(){
        //update some record


        //---> i want to call the acionCreate() from the AuditTrailController from here
        //create an audit entry telling the user "updated"
        }

}

-The Yii:app() doesn't do the trick..(or ive used it wrongly)..

I've searched for many answers but they say it's "evil" to call another controller from one..i saw terms like using Component..other say it should be done in the model... but i have no idea.. i'm new in OOP and Yii so i'm in a struggle here.. help me out thanks.. but if the above is possible then a sample code would be really helpful thanks!

Was it helpful?

Solution

you can call action in another controller in this way

//here is you controller with action

class AuditTrailController extends Controller
{

    public function actionCreate()
    {
    //do create the trail   
    }

}

//here controller where you want to use your action

class StudyController extends Controller {

        public function addRecord(){
               $controller = new AuditTrailController($this->id);
               $controller->actionCreate();
        }
}

And don't forget add to config this line to import

    'application.controllers.*',
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top