开发者

CakePHP: Query in Component?

开发者 https://www.devze.com 2022-12-16 12:24 出处:网络
Got question, maybe even problem while creating CakePHP Component. Basically, I need to implement few quesries that can be accesses everywhere in my layout (si开发者_C百科debar statistics and so).

Got question, maybe even problem while creating CakePHP Component. Basically, I need to implement few quesries that can be accesses everywhere in my layout (si开发者_C百科debar statistics and so).

When I try to query in Component, I got error about calling function on a non-object.

Damn, can anybody explain me this one?

Cheers!


Are you doing something like this?

class MyComponent extends object {
  function startup(&$controller) {
    $this->controller = $controller; // Stores reference Controller in the component
  }
  function common() {
    $data = ClassRegistry::init('MyModel')->myQuery(); // Call the query on the model
    $this->controller->set(compact('data')); // Sets data from myQuery in view
  }
}


At the risk of sounding pedantic, you'd be violating MVC pretty egregiously by doing this. If you're okay with that, you can use App::import() to load any model from anywhere in your app (http://book.cakephp.org/view/531/Importing-Controllers-Models-Components-Behaviors-).

If you're interested in attempting to retain the MVC structure, we may be able to help with some more information about the queries you need to run in that generic manner.

0

精彩评论

暂无评论...
验证码 换一张
取 消