使用cakephp中的模型类中的控制器方法 [英] use controller method from model class in cakephp

查看:118
本文介绍了使用cakephp中的模型类中的控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的cakephp项目中,我使用模型类的afterSave()方法。在这个方法中,我想调用位于app_controller文件中的另一个方法。

  class MyModel extends AppModel {

var $ name ='MyModel';

function afterSave($ created){

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

}

这里updateData



上面的代码不起作用,所以如何在这种情况下调用updateData()。


解决方案

div>

这是强烈不推荐,但它可以做反正...你应该尝试作为deizel说,并将该方法移动到AppModel或任何其他特定的模型...



您可以使用此函数



App :: import()检查

pre> class MyModel extends AppModel {

var $ name ='MyModel';

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

}

这是正确的加载方式一个类里面另一个地方,它不应该...仍然你可以使用include或必需和创建一个类的实例,因为这是php。


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

解决方案

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.

这篇关于使用cakephp中的模型类中的控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆