[英]CakePHP: Loading Model in Controller
我想在我的控制器中加载我的模型。 该模型未与数据库中的表关联,因此可能无法遵循CakePHP的ORM。
我目前有以下代码(这是我的模型):
<?php
namespace App\Model\Json;
use Cake\Filesystem\File;
class Processes
{
public static function getData()
{
$file = new File('process_data.json');
$json = $file->read(true, 'r');
$jsonstd = json_decode($json);
// remove STD classes
$json2array = json_decode(json_encode($jsonstd), true);
$cpu = array();
foreach ($json2array as $key => $row)
{
$cpu[$key] = $row['cpu_usage_precent'];
}
array_multisort($cpu, SORT_DESC, $json2array);
// return data
return $json2array;
}
}
我通过以下代码(在控制器中)调用Model:
$json2array = $this->Processes->getJson();
$this->set('data', $json2array);
我无法以某种方式在我的控制器中调用它。 我一直收到以下错误:
应用程序中的某些Table对象是通过实例化“Cake \\ ORM \\ Table”而不是任何其他特定子类来创建的。
请尝试更正以下表别名的问题:
流程
下面是一个示例,如何在CakePHP 3.x
访问没有CakePHP ORM
Model
在模型: Processes
在文件
/path_to/src/Model/Table/Processes.php
namespace App\Model\Table; #Define the namespace
use Cake\Filesystem\File;
class Processes{
public static function getData(){
/*Your codes here*/
}
}
在控制器: Bookmarks
在文件
/path_to/src/Controller/BookmarksController.php
namespace App\Controller;
use App\Model\Table\Processes; #Using PSR-4 Auto loading
use App\Controller\AppController;
class BookmarksController extends AppController{
public function tags(){
$data = Processes::getData(); #Now you can assess Data
}
}
以下是有关使用PSR-4自动加载的详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.