Tijmen
Tijmen

Reputation: 149

Where to put certain logic in CakePHP

I've recently started to rewrite a project I did a few years ago using CakePHP. I'm trying to do everything 'right' this time, so maybe someone get give me a a pointer on doing to the following:

I'm showing a simple table from a table using Model->find('all') in the View. There are two boolean fields in this table, that together make up something I need to show to a user. So: 0x0 = 'A', 1x0 = 'B', 0x1 = 'C', 1x1 = 'D'. Where should I put this logic? I've been thinking about the following methods:

  1. The view
  2. A View helper
  3. The controller
  4. Something in the Model so that Model->find('all') outputs this value (is that even possible?)

This task might seem trivial, but I think it might learn me getting this project organized and maintainable from the start.

Thanks!

Upvotes: 3

Views: 498

Answers (4)

Leo
Leo

Reputation: 6571

I put this kind of logic in the view if it is something that is going to determine rendering style. In that way, the designer has maximum access and can style accordingly.

On the other hand, if the two columns only exist for convenience in datamodelling, put it in the model. The designer shouldn't even be aware of other possibilities!

Upvotes: 0

Qwerty
Qwerty

Reputation: 1742

Well, it depends on the type of logic for making up final table (is it presentation or business?).

Imagine you add new type of UI, for example command line interface. How would you show your table there? The data passed to View has to be same for both HTML and console presentations. So the logic which is responsible for preparing that data - is business logic and it should be placed in Model. The logic responsible for displaying the data should be placed in View (maybe in view helper if it's used more than once).

And never place this kind of logic in Controller.

Upvotes: 5

grapefrukt
grapefrukt

Reputation: 27045

If it's something you're going to use all over the place I would put it in the model. You can either put a method on the model that gives that value back or loop over all the rows you've retrieved in an afterFind callback and set it as a proper field.

Upvotes: 1

poo
poo

Reputation: 1115

In the controller! The methods from the model comes in the controller. The view is just for output( like HTML UI programming.)

Upvotes: -1

Related Questions