Thursday, October 28, 2010

Re: Why only pass $data to view instead of model instance?

i agree that this would be handy at some point
but it helps to keep the layers separated
otherwise one would be tempted to read/write to the DB in the views

i usually use static model methods for this

/** static **/
function isRent($data) {
return !empty($data[$this->alias]['rent_user_id']);
}

you can now call it anywhere (model, controller, behavior, view)
be sure you pass the data, though

PS: actually it has another upside. you can use it inside iterations
as well - like in foreach() loops in the view


On 28 Okt., 11:13, psybear83 <psybea...@gmail.com> wrote:
> Hi everybody
>
> Something I don't really like in CakePHP is that one seems to be
> always only working with the $data array instead of "real" model
> instances. I hate to name Ruby on Rails again, but in RoR, I normally
> pass a model instace to a view, and there I can do handy stuff like
> calling methods on that model etc.
>
> But in CakePHP it seems normal to only pass the $data of the model. So
> I can't call any methods on it.
>
> For example, I have a Medium (e.g. a book) model that can be rent. It
> has a method isRent():
>
> function isRent() {
>   return !empty($this->data['Medium']['rent_user_id']);
>
> }
>
> So when the rent_user_id is empty, the medium is rent to that user.
>
> It would be really handy to use this method also in a view, but it
> seems I can't. Is the only solution to set another parameter for the
> view, like so?
>
> $this->set('isRent', $model->isRent());
>
> Thanks for help
> Josh

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: