Prashant
Prashant

Reputation: 5461

use controller method from model class in cakephp

In my cakephp project, I use afterSave() method of a model class. In this method, I want to call another method that is located in app_controller file.

class MyModel extends AppModel {

        var $name = 'MyModel';

        function afterSave($created) {

            $this->MyController->updateData();          
        }

}

Here updateData() is located in app_controller file, which is extended by MyController controller.

The above code does not work, so how can i actually call updateData() in this case..

Please guide.

Thanks

Upvotes: 2

Views: 6017

Answers (1)

api55
api55

Reputation: 11420

This is strongly NOT recommended but it can be done anyway... You should try as deizel says and move that method to AppModel or any other particular model...

you may use this function

App::import() check the book here to see how to use it

in your example:

class MyModel extends AppModel {

        var $name = 'MyModel';

        function afterSave($created) {
            App::import('Controller', 'My');
            $something = new MyController;
            $something->updateData();          
        }

}

This is the correct way to load a class inside another place where it shouldn't be... Still you may use include or required and create an instance of the class since this is php.

Upvotes: 5

Related Questions