slacky
slacky

Reputation: 101

CakePHP Shell, how to use a controller action

Using cake 2.1.1. I'm trying to make a cron job to execute an action from a controller. Which is right way to do this? I have the OffersController with an action called admin_test. I'd wish to run this action every 2 hours. For the moment I made a shell in app/Console/command/SyncapiShell.php:

class SyncapiShell extends AppShell {
    public $uses = array('Offer');

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

But I get a SQLSTATE[42000] Syntax error or access violation trying to execute the shell. I'm also using the admin routing, the auth component and ACL. How does the shell work? It ignores the authentication and the acl rights? Normally the admin_test action may be accessed only by specific authenticated users.

Thank you

Upvotes: 1

Views: 3878

Answers (1)

floriank
floriank

Reputation: 25698

No this is not the right way to do but more likely a strong indicator that your app architecture is not good. You should have fat models and tiny controllers.

Shell:uses will work like the uses property of a controller and load models. In Controllers you should use the model associations and not load thousands of models using uses.

Refactor your controller method and move the code into the Offer model.

LayerCake

And why does a shell need authentication or admin routing? A shell is, as the name says, a shell program, NOT a website. Authentication is basically done by the OS and user who runs the script. Only people who have access to the shell will have be able to run it anyways.

Upvotes: 5

Related Questions