What is the correct way to retrieve Database information using a custom cakePHP helper?
the short answer is - no. You need to pull the data from the Controller and pass it to the view from where the helper will get it.
I had the same heretic ideas in the beginning when I started with MVC pattern. :)
There is no correct way for finding database info in a helper, and you should avoid it.
But there is a way of doing it in helpers and views: $posts = ClassRegistry::init('Post')->find('all');
Again, you are recommended not to use Models in views and you will thank yourself later.
i dont agree with the concept that i may not use database result in helper. because suppose i am trying to show dynamic links in side bar or in footer i have to put the result in each method in my every controller. so it is easy to call the function in helper that make a element and call that helper in it.
now you can call database results in helper just like this
ClassRegistry::init('Post')->find('all'); As written by fahad.
and for the note if you have made some method in the post model you can call that too like this ClassRegistry::init('Post')->custom_function_name($params);
Yes Anthony i have same question. i could not find perfect answer. CakePHP allow to call model function from help like this ClassRegistry::init('Post')->find('all'), but yes myself confused whether it is right approch to use that or not.
Yes i agree with Dip12320001, i think for this kind of needs cakephp provide support to call model from Helpers...but yes we need expert comments on it. means what community say about this kind of needs and what is the best way to cater this needs in cakephp (by reusing the code). yes As per my knowledge, there instead of putting model calling in helper we can put that in Appcontoller, which every controler can use....but still we have to call APPcontroller funtion from every controller which is also repeatation of same code. So is there anybody who can put some light on it. Is Dip12320001 approch is really a right approch, i would like to hear as i am working on a product and i have same kind of situation.
thanks
If you want to have dynamic links on every page then write a component that grabs the links from the db, and then sets the variable in the controller so the view knows about it. Have you AppController use the Component and if you write it correctly you won't ever have to include it in any action.
http://github.com/markstory/cakephp_menu_component
Or...
Write an element that uses requestAction and caches the menus. Include this element in your view when you need it.
http://bakery.cakephp.org/articles/view/creating-reusable-elements-with-requestaction
Or...
Run the code in your AppController::beforeFilter method and assign the view variable that way.
http://book.cakephp.org/view/984/Callbacks
Any of these methods are MVC correct while instancing a model in your view is not correct and there isn't any room to argue about it. Do it the right way and save yourself the headaches later while learning about how CakePHP can keep your code DRY