Calling AppModel function in AppController for cakephp
Asked Answered
Q

3

6

I have a function that I want all of my controllers to be able to use, so I have defined it in AppController. Now part of what this function will do has no business being in a controller and so it should be in a model, but since this is a universal operation, it only seems correct that it be in AppModel. My function looks as followed:

class AppController extends Controller {

    public function i_need_this_everywhere ($term) {
        // do some stuff

        // this is the line that is an issue
        // it seems like this should be simple and work, but no variation of this is working
        $value = $this->App->get_some_cool_data($term);

        return $value;
    }

}

I simply want to be able to call an AppModel function from AppController. I have tried the following:

// I have tried several variations of this.
$this->loadModel('AppModel');
$this->AppModel->get_some_cool_data($term);

But this then complains about the lack of database table called AppModel, at which point, in AppModel I tried setting:

public $useTable = FALSE;

But that blows the whole app up so... Now I am out of ideas. Any help would be greatly appreciated.

Quinlan answered 13/2, 2014 at 21:25 Comment(1)
Sounds like you wanted to create a Model/Universal.php but instead went with option kaboom :).Arvizu
G
7

All your models should inherit from AppModel, so why not call $this->AnyModel->get_some_cool_data($term); instead? AppModel should be pretty much be an abstract class anyways -- you pretty much never want to instantiate it, you just use as a base class.

Also, that should be $useTable = false; not $usesTable. Remember that, again, all your models should inherit from AppModel, so all your models will end up not using the database as well, which is probably the source of your errors.

Gerda answered 13/2, 2014 at 22:35 Comment(3)
Right, but the trick is I'm trying to call AppModel in AppController, I think I could call $this->loadModel('SomeModel'); $this->SomeModel->fucntion() in AppControl. But that seems like quite the hack.Quinlan
I don't really see it as a hack. Sometimes you have to load different models in the AppController for some reason. Though if you really don't need to use any model in the AppController besides for this, you could also do ClassRegistry::init('SomeModel')->function();, which will only load the model locally (it won't be saved as a property of the AppController.)Gerda
I agree witk @Kai. If you need exact and clear definition of SomeModel in specific controller you can add yor own property into AppController, and set the model name in each controller there. With some default model so that you don't need to edit all your controllersMusso
Q
0

To solve this for me, the solution that worked but I am unsatisfied with is to place these functions in the AppModel and then call them from any model like so:

$this->WhatEverModelIAmIn->the_general_function_I_need($some_argument);
Quinlan answered 31/3, 2014 at 0:4 Comment(0)
W
0

If you want to use a method in AppController to call a method in AppModel, using the specific model class that belongs to the invoked controller class, then I believe (I could be wrong) this is how you should do it:

class AppController extends Controller {
    public function foo() {
        $this->{$this->modelClass}->bar();
    }
}

$this->modelClass is the class of the model that belongs to whichever controller inherited AppController, so that will invoke AppModel as whichever model it's supposed to be at the time.

Washy answered 18/3, 2022 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.