问题描述
我想将我的模型加载到我的控制器中。该模型未与数据库中的表关联,因此它可能无法遵循CakePHP的ORM。
我目前有以下代码(这是我的模型) :
<?php
命名空间App\Model\Json;
使用Cake\Filesystem\File;
类处理
{
公共静态函数getData()
{
$ file = new File('process_data .json');
$ json = $ file-> read(true,‘r’);
$ jsonstd = json_decode($ json);
//删除STD类
$ 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 $ json2array;
}
}
我通过以下代码调用模型(在控制器):
$ json2array = $ this-> Processes-> getJson();
$ this-> set(’data’,$ json2array);
我无法以某种方式在Controller中调用它。我一直收到以下错误:
以下是示例,如何访问 在模型中: 在控制器中: I would like to load my Model in my Controller. The model is not associcated with a table in the database, thus it is probably not able to follow CakePHP's ORM. I have the following code currently (this is my Model): I call the Model through the following code (in the controller): I am not able to call it in my Controller somehow. I keep getting the following error: Here is an example, How to access In Model : In Controller : 这篇关于CakePHP:在控制器中加载模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!在
CakePHP 3.x中没有
CakePHP ORM
的模型
过程
命名空间App\Model\Table; #定义名称空间
使用Cake\Filesystem\File;
类流程{
公共静态函数getData(){
/ *此处输入代码* /
}
}
书签
命名空间App\Controller;
使用App\Model\Table\Processes; #使用PSR-4自动加载
,使用App\Controller\AppController;
类BookmarksController扩展了AppController {
公共功能tags(){
$ data = Processes :: getData(); #现在您可以评估数据
}
}
<?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;
}
}
$json2array = $this->Processes->getJson();
$this->set('data', $json2array);
Model
without CakePHP ORM
in CakePHP 3.x
Processes
namespace App\Model\Table; #Define the namespace
use Cake\Filesystem\File;
class Processes{
public static function getData(){
/*Your codes here*/
}
}
Bookmarks
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
}
}