Sunday, October 3, 2010

Re: models: fundamental question referring to member variables

On Sun, Oct 3, 2010 at 6:56 PM, DerBjörn <b.unkhoff@googlemail.com> wrote:
> The user table has a column 'birthtime', but in my page i want to show
> the age in years and days of the user instead.

The you should make a view helper for this, and not bother the data in-transit.

class AgeHelper extends AppHelper {} (I think, I don't have the
inclination ATM to ensure I'm accurate).

> So when i retrieve the data of the database i first need to set the
> birthtime:
>
> $this->Age->setBirthtime($data['User']['birthtime']);
> to be then able to access to calculated years and days:
>
> $user->getAge()->getYears();
> $user->getAge()->getDays();
>
> The same the other way round:
> If i want to randomize the birthtime of the user with my solution i am
> able to do it like that:
> $user->getAge()->randomizeBirthtime();

If you go with my Helper idea, I see no point in any of this. The data
should stay exactly as it came from the database, and your Helper
handle displaying how you want it.

> But when i want to save the user of course first i have to get the
> random birthtime out of its class and put it in the array:
> $this->data['User']['birthtime'] = $this->Age->getBirthtime();
> $this->save($this->data);
>
> Something like that, sorry about my pseudo code.
> The problem is that the most of my member varables are classes
> actually to be able to randomize or do calculations with their values.

And if you go with my method, you don't have to worry about messing
with the data at all. If they change it, you do the same calculations
in the model's beforeSave. Or the controller, whichever holds Cthulhu
off another night.

--
Jack Timmons
@_Codeacula

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: