Tuesday, December 29, 2009

Re: coding practices with models

I feel the same way sometimes. It is tempting to use model methods as
$record->method() in a way similar to an object-based ORM (e.g. Rails,
where class methods are used for finding and instance methods are used
for record manipulation).

But the way Cake works it is a bit dangerous and I would recommend
following John's advice.

/Martin


On Dec 28, 1:39 pm, John Andersen <j.andersen...@gmail.com> wrote:
> I use the model class as the maintainer of the individual record and
> the provider of a collection of records.
>
> So in your banana example, my model would know what to do when a
> controller stated that peeling the banana was required :)
>
> Whether or not to read the banana into the model depends on the
> situation you are in - is the banana currently being viewed, then the
> model already has the banana - but if the banana was just one of many
> selected by the user for peeling, then the model would need to be
> provided with the id of the banana to peel, read it from the database
> and then peel it :)
>
> I would not let the controller know whether to read the banana in the
> model, that is the models responsibility, to ensure that the correct
> banana is being peeled :) - so the controller would just provide the
> id to the model through the peel method and the model compares whether
> the current banana is the requested one!
>
> I usually separate the model methods into those that works with an
> individual record and those that work with a collection of records.
>
> On Dec 28, 4:14 am, Christian <cdamiani...@gmail.com> wrote:
>
>
>
> > When i come across situations that I feel needs to be coded inside the
> > model I'm not sure whether (form the perspective of the controller or
> > a different model) to treat a model as an object, or just as a
> > collection of class methods.
>
> > For example:
>
> > If I want to peel a banana..
>
> > Is it correct to instantiate the Banana
> > $this->Banana->read(null,$id);
>
> > and then call the method
> > $this->Banana->peel();
>
> > and in banana.php:
> > function peel() {
> >    //since this method was called on a specific instance
> >    //it should have access to it's private variables (tuple from the
> > database)
>
> > }
>
> > OR do i just call the method on the id and sort that out in the model
>
> > $this->Banana->peel($id);
>
> > then...
> > function peel($id) {
> >    //need to instantiate the banana first before we can operate on it
> >   $this->Banana->read(null,$id);
>
> > }
>
> > I'm curious about the practices of some of you regular cakers.
> > thanks

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

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

No comments: