What kind of Model-methods do you write?

2009-10-17 Thread Martin Westin

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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: What kind of Model-methods do you write?

2009-10-17 Thread robustsolution

I think I understand your question.

yes by default it seems that CRUD/FIND are row level.

but for custom pagination query or for one to many relationships
(threads vs posts) it is not the same

I liked $this-id=;

In case of a single item/SSTabs, I would use the containable behavior.
In case of pagination I use exactly the custom pagination query
technique without using the built in query() method because it is
slower. I integrate a prepared class and I use the same loaded
connection instance that cake is already using otherwise I create it
In case of anything else I try to mimic the cake way that transforms a
complex query to a set  of simple queries.

I hate parameters and I don't know why

but in general I tries to put (as much as possible) the crud/search
functions in the controller and use the models as aggregated things.

I try as much as possible to do the things the cake way, but when I
see a loss in performance (as in custom pagination query), I integrate
my favorite PHP classes as vendors inside the application.

appmodel is very important man... abuse it

On Oct 17, 6:45 pm, Martin Westin martin.westin...@gmail.com wrote:
 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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---