user1296259
user1296259

Reputation: 521

trying to get a console script in cakephp to access a controller

So, I have been trying to get a console script working in CAKEPHP. But it won't let me access controllers using the $uses member.

I have tried assigning an array to $uses, and a non array to $uses, both outside and inside of a method inside of my JobManagerClass, and nothing works.

Right now, I have...

<?php

class JobManagerShell extends 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)));
    }*/
}

}

?>

and that gives the error:

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

If I try changint the line that says:

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

to

$this->uses = "Job";

I get the error:

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

I've been trying to find an answer to this, but I can't seem to.

Upvotes: 0

Views: 710

Answers (2)

Farooq Bellard
Farooq Bellard

Reputation: 78

in your shell script/file, which may be here(src/shell/your-script-file). (for cake-php 3.X)

on top use this:

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

and under initialize() function create object of your controller.

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

after this you can call any of your controller function in shell file.

for-example i have get_reports_details() function in my ReportsController so i will call this function like:

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

Upvotes: 1

Tomasz Ferfecki
Tomasz Ferfecki

Reputation: 1263

If you need use other model use:

$this->loadModel('ModelName')

Read more: loadModel in CakeBook

Also, I don't recommend using controller action in shell. Use model methods instead. (In case if you don't have in model things that you need - move them to model [this is their place].)

Upvotes: 0

Related Questions