Php – Best practice in CakePHP for saving data, using models in component


I'm writing a Cake component and it seems to make sense that I use it for saving data rather than doing so in a controller. In the manual it says using models in a component is discouraged, but the other way of doing it would mean I'd be repeating code in the controller.

The component basically analyses a load of data from various sources and will then insert data for various models.

Best Solution

If the component analyses a load of data from various sources, it can probably be written as a behaviour.

If not, and you have no other choice but to use a model in your component, be sure to do it properly:

$this->ModelName = ClassRegistry::init('ModelName');

I personally don't see a problem with this, since the core components (such as the Acl component) do that. As long as your component handles the application logic and models handle data, you'll be just fine.

Related Question