Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakePHP: Can you query the database from a helper class?

Hi just need to know whether you can query the database from inside a helper class, whether you should and how you do it.

Thanks

like image 359
geoffs3310 Avatar asked Oct 27 '10 08:10

geoffs3310


1 Answers

You could, by passing a reference to the Model into the View as a variable via $this->set() and then querying it...but you shouldn't. It's messy ;-)

CakePHP uses the MVC model, and helpers are part of the View (the V of MVC) - their job is purely to display the (already available) information passed to it from the controller.

If your view needs extra information , then your controller should have already queried the Models to get it.

I'd suggest you read up on the MVC model if you're not familiar with it, then some refactoring might be in order!

like image 103
AdamGiles Avatar answered Oct 02 '22 15:10

AdamGiles