试图在cakephp中获取一个控制台脚本来访问控制器

时间:2014-05-26 21:35:15

标签: php cakephp controller console

所以,我一直试图让一个控制台脚本在CAKEPHP中运行。但它不允许我使用$uses成员访问控制器。

我尝试将一个数组分配给$ uses,并将一个非数组分配给$ uses,在我的JobManagerClass中的方法的外部和内部,没有任何效果。

现在,我有......

<?php

类JobManagerShell扩展了AppShell {

public $uses;

public function main() {
    $this->_processJobs();
}

public function _processJobs() {

    $this->uses = array("Job");

    $jobs = $this->Job->find("all");

    foreach ($jobs as $job) {
        if ($job["is_running"] == 1) exit;
    }

    foreach ($jobs as $job) {
        $id = $this->Job->id = $job["id"];
        $this->Job->saveField(array("Job" => array("is_running" => 1)));
        exec($job["command"] . "2> errors.txt");
        $errorsFile = fopen("errors.txt");
        $errorsText = fread($errorsFile);
        fclose($errorsFile);
        $this->Job->delete($this->Job->id);
        $this->uses = array("Error");
        $this->Error->save(array("Error" => array("job_id" => $i, "error" => $errorsText)));
    }*/
}

}

&GT;

,这给出了错误:

PHP Fatal error:  Class 'AppModel' not found in /home/webdev/webroot/Cake/lib/Cake/Utility/ClassRegistry.php on line 185

如果我尝试改变说:

的行
$this->uses = array("Job");

$this->uses = "Job";

我收到错误:

Notice Error: Undefined property: JobManagerShell::$Job in [/home/webdev/webroot/Vehicle_Scrapper/lib/Cake/Console/Shell.php, line 491]

我一直试图找到答案,但我似乎无法做到。

2 个答案:

答案 0 :(得分:1)

在您的shell脚本/文件中,可能在此处(src / shell /您的脚本文件)。 (对于cake-php 3.X)

最上面使用此:

use Cake\Core\App;
use App\Controller\ReportsController; //(path to your controller).

并在initialize()函数下创建控制器的对象。

public function initialize() {
    parent::initialize();
    $this->reports = new ReportsController();
}

此后,您可以在shell文件中调用任何控制器函数。

例如,我在ReportsController中具有get_reports_details()函数,因此我将调用此函数,如:

publict function main(){
 $this->reports->get_reports_details();
}

答案 1 :(得分:0)

如果您需要使用其他型号:

$this->loadModel('ModelName')

了解详情:loadModel in CakeBook

另外,我不建议在shell中使用控制器操作。 请改用模型方法。 (如果你没有你需要的模型物品 - 将它们移到模型[这是他们的位置]。)