Thursday, October 28, 2010

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

i need to correct myself

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

and you simply use it this way:

//in views
if (Model::isRent($book['Book'])) {...}

this way it it can be used in any iteration as well


On 28 Okt., 12:45, Joshua Muheim <psybea...@gmail.com> wrote:
> Thanks for your reply.
>
> You mean I should stick the code above into my view (including the
> App::import(...))? I agree with you, this would quite break the MVC
> convention. But what speaks against using this code in the
> controller...
>
> $this->set('book', $this->Book);
>
> and then just using the $book instance in the view like so?
>
> <?php
>   echo "Rent: ".$book->isRent();
> ?>
>
> IMHO, this wouldn't break MVC, but still CakePHP doesn't suggest it,
> as far as I can see. Why?
>
>
>
>
>
>
>
> On Thu, Oct 28, 2010 at 12:36 PM, Hashim Ikram <l050...@gmail.com> wrote:
> > Hi Josh,
> >          It is better to use Model functions in controller according to MVC
> > convention, but if you want to use model function in view you can use it in
> > this way.
> > /* ------------------------------------------------------------- */
> > App::import('Model',array('Book'));
> > $bookObj = new Book();
>
> > $isRent = $bookObj->isRent();
> > /*-----------------------------------------------------------------------*/
>
> > hope it helps you but still I would say that as you are useing a MVC
> > framework so you should get Data in controller and then pass it to view. But
> > really sometimes we have mold or break the convention rules so choice is
> > yours :)
>
> > Regards
> > Hashim Ikram
>
> > On Thu, Oct 28, 2010 at 3:10 PM, euromark <dereurom...@googlemail.com>
> > wrote:
>
> >> 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 sitehttp://cakeqs.organd 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
> >> athttp://groups.google.com/group/cake-php?hl=en
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organd 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

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: