Sometimes it's too inconvenient and/or inefficient to do all possible
data formatting in model functions (like afterFind). In my opinion,
formatting data with a helper function is often the way to go. It
would be a lot easier if find queries returned data as objects instead
of arrays (then Jerry's $p->getTiming() example could be used on an
'active record' instead of creating a new class instance), but that's
the way the cookie crumbles.

Jerry's solution is really only intensive if (A) model caching is
disabled and (B) a new instance is created with every helper call
(which it is). Something like this might be better (in the helper):

function getTiming() {
    if (!$this->_project) {
        App::import('Model', 'Project');
        $this->_project = new Project();
    }

    return $this->_project->getTiming();
}

Obviously that's a bit of a pseudo-code example, but it helps cut down
on the new class instances, especially if getTiming() will be called
50 times in a foreach() loop. I have helpers with similar functions
that generate router URL arrays for some of the more complex parts of
my projects, mainly so that the web designers - who aren't really Cake
experts - can link to something easily.

But like you said, Jeremy, it's also a good idea to do data formatting
in the model. Either approach works. It's just personal preference.

- Jamie

On May 7, 12:36 am, Jeremy Burns <[email protected]> wrote:
> Wow - that'a very intensive solution. Just do it in the model in the first 
> place.
>
> Jeremy Burns
> [email protected]
>
> On 7 May 2010, at 02:52, Jerry wrote:
>
>
>
>
>
> > Hi Brian:
>
> > Abstract it into helper is not much different in view.
> > In this case I will do like this:
>
> > App::import('Model', 'Project');
> > $p = new Project;
> > $p->getTiming();
>
> > For your reference.
>
> > --
> > Jerry
>
> > On 5月7日, 上午5時40分, bmcelhany <[email protected]> wrote:
> >> Hello,
>
> >> I have a model (Project) that has a projected_end_date field. What I'd
> >> like to do in the view is calculate the number of days from the
> >> current date until the projected end date and display how much time is
> >> left (or how many days overdue). This data really only needs to be in
> >> a single view, so I have no problems just embedding the logic directly
> >> into the view...I'm doing that now and it works fine.
>
> >> Even though it's working, it just feels wrong. It seems like this
> >> logic really should be in the model itself since it's data (calculated
> >> data and not stored in a table, but still data).
>
> >> Is this possible? I tried creating a getTiming() function in my
> >> Project model, but I couldn't figure out any way to call it from my
> >> view:
>
> >> echo $project['Project']->getTiming()
> >> echo $project->getTiming()
> >> echo $this->Project->getTiming()
> >> etc.
>
> >> I may end up just abstracting the logic out into a helper and calling
> >> that in the view. Any thoughts? Thanks!
>
> >> -Brian
>
> >> Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp 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 [email protected]
> >> To unsubscribe from this group, send email to
> >> [email protected] 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 [email protected]
> > To unsubscribe from this group, send email to
> > [email protected] 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected] For more options, visit this group 
> athttp://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 [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to