CakePHP:在控制器中加载模型 [英] CakePHP: Loading Model in Controller

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

问题描述

我想将我的模型加载到我的控制器中。该模型未与数据库中的表关联,因此它可能无法遵循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中调用它。我一直收到以下错误:


应用程序中的某些Table对象是由
实例化 Cake\ORM

请尝试更正以下表别名的问题:



进程



解决方案

以下是示例,如何访问 CakePHP 3.x中没有 CakePHP ORM 的模型

在模型中: 过程


在文件 /path_to/src/Model/Table/Processes.php




 命名空间App\Model\Table; #定义名称空间

使用Cake\Filesystem\File;

类流程{
公共静态函数getData(){
/ *此处输入代码* /
}
}

在控制器中: 书签


在文件 /path_to/src/Controller/BookmarksController.php




 命名空间App\Controller; 

使用App\Model\Table\Processes; #使用PSR-4自动加载
,使用App\Controller\AppController;

类BookmarksController扩展了AppController {
公共功能tags(){
$ data = Processes :: getData(); #现在您可以评估数据
}
}




以下是有关使用PSR-4自动加载的详细信息



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):

<?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;
        }
}

I call the Model through the following code (in the controller):

$json2array = $this->Processes->getJson();

$this->set('data', $json2array);

I am not able to call it in my Controller somehow. I keep getting the following error:

Some of the Table objects in your application were created by instantiating "Cake\ORM\Table" instead of any other specific subclass.

Please try correcting the issue for the following table aliases:

Processes

解决方案

Here is an example, How to access Model without CakePHP ORM in CakePHP 3.x

In Model : Processes

In the file /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*/
    }
}

In Controller : Bookmarks

In the file /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
    }
}

Here is the details about Auto loading with PSR-4

这篇关于CakePHP:在控制器中加载模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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