2017-03-16 112 views
1

我想在我的控制器中加载我的模型。该模型不会与数据库中的表关联,因此可能无法关注CakePHP的ORM。CakePHP:在控制器中加载模型

我已经目前下面的代码(这是我的模型):

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

我不能以某种方式在我的控制器中调用它。我不断收到以下错误:

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

+0

没有一个CakePHP的用户,但我希望'AppModel'是与ORM一个CakePHP的类。因为你的班级没有使用orm,所以''AppModel'不是固有的' – Steve

+0

你是怎么在你的控制器中调用它的?如果你使用'loadModel',不要。加载Table类,这不是一个表。 – ahoffner

+0

我不会通过'loadModel'加载它,而是通过'$ this-> Processes',这很可能因为它与ORM相关而不起作用。 – h0sfx0

回答

0

下面是一个例子,如何访问Model没有CakePHP ORMCakePHP 3.x

在型号: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*/ 
    } 
} 

在控制器: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