我想在cakephp3的控制器中的特定功能中加载多个模型吗?

时间:2019-08-22 05:10:16

标签: cakephp cakephp-3.0

我试图在cakePhp3中设置$ uses,它在cakePhp2.6中起作用,但在3中不起作用

public $uses = array('model1', 'model2', 'model3', .... );

但我收到类似

的错误
{
    "message": "Call to a member function find() on boolean",
    "url": "/JobPosts/Test/1042.json",
    "code": 500,
    "file": "/home/task24/public_html/clientApi/src/Controller/JobPostsController.php",
    "line": 1304
}

2 个答案:

答案 0 :(得分:0)

您在Cake 3中不再使用$uses

$this->loadModel('MyModel');

答案 1 :(得分:-1)

尝试使用 array_map 函数

加载所有模型
array_map([$this, 'loadModel'], ['Product', 'Orders', 'OrderItem']);   //Pass each element of the array to loadModel with is accessable through $this object.

或者在您的 AppController 中创建一个函数,例如

function loadModels($models = []) {
   foreach($models as $model) {
       $this->loadModel($model);
   }
}
//Then you can access this function anywhere from any controller function (Controller which are extending AppController)
$this->loadModels(['Products', 'Orders', 'SO-ON']);