Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

cakephp

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.

like image 926
MSR Avatar asked Apr 16 '09 10:04

MSR


1 Answers

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.

like image 114
dr Hannibal Lecter Avatar answered Sep 28 '22 19:09

dr Hannibal Lecter