tags:

views:

925

answers:

3

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.

A: 

In my opinion, DRY is the critical path. To that end, I see two options:

  1. Instead of saving the data in the component, return it to the controller in a raw form that can be saved from the controller.
  2. Go ahead and import the model into the component and do what you need to do. It's not encouraged, but they do make it reasonably easy.

If it comes down to a hard choice, I'd err on the side of keeping my application DRY rather than maintaining a strict MVC discipline.

Rob Wilkerson
+2  A: 

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.

dr Hannibal Lecter
A: 

One could argue that if you want to write data generated in a component and have the ability to package the component up for distribution it would make sense to load a model in your component(especially if the model is part of your component).

For example I have a authorize.net AIM (credit card authorization) component.

From my controller I call the components charge function. Authorize.net sends me a response (within the component) and according to DRY I would want to save the data within the component so I don't have to repeat saving the data from wherever the charge function is called.

Another plus to straying away from convention would be the fact that I could use a model from within the component and not have to have a model in the app.

What do you think?

shennyg