Saturday, October 17, 2009

What kind of Model-methods do you write?

Dumb question? I hope not. I wanted to see which is the most popular
way of writing methods in models. And by "which way" and "what kind" I
am referring to if the methods assume and require a loaded "row" from
the database or not.

Better use an example. Drawing from the ever present blog tutorial,
assume you want to change a post from draft to published. That is: a
simple method that operated on the model data. Would you write:

function publish() {
$this->saveField('published', 1);
}

or

function publish($id) {
$this->id = $id;
$this->saveField('published', 1);
}

Note: both very condensed and without any returns or error-checking
just to keep things short.

The important bit is wether you prefer to write methods that assume
$this = the table or $this = a row. Variations of save and find will
always be table-level methods but a lot of other methods could be
written either way. Row-level methods would be more natural in
something like rails since results are objects.

From the controller the first method above would be called like this:
$this->Post->read($id); // or find('first',..)
$this->Post->publish();

and the second:
$this->Post->publish($id);


Possibly the example is a poor one but I hope you can discern what I
am driving at.
I have tried writing both ways. I like writing row-level methods but
they can be problematic because of Cake's data handling.

/Martin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate