use cakephp component inside a model

user765368 picture user765368 · Mar 19, 2012 · Viewed 12.6k times · Source

How do I use a component that I created in cakePHP inside one of my model classes? Is this possible?

If so, please let me know how I can do so

Answer

burzum picture burzum · Mar 19, 2012

It is possible but pretty bad practice in a MVC framework. You should re-think and re-organize your code if you think you need to use the component in a model because something is cleary wrong then.

A component is thought to share code between controllers, only between controllers.

To share re-usable code between models it would be a behavior. For a view it would be a helper.

If you have some really generic code it should be a lib or put it in the Utility folder / namespace or create a new namespace. Check the existing classes there to get an idea what to put in there.

No code was provided so it is not possible to give any real recommendation on how to refactor it. However the way you want to use the existing code won't work in the MVC context, so time to rethink your approach of whatever you try to do.