like to add my 2 cents.
If you insist on translating binary bool values into human readable
values, you should pair your afterFind() method with a beforeSave()
method. beforeSave() will then translate back from the 'human' values
into the correct format for the database. Note that this is more
commonly used when your database values aren't a good fit (your app /
data requirements have changed, but you are unable to modify the
database schema).
Personally, I usually just leave my values as machine readable until
they're ready to be presented to the view. For example: if a user can
select which states they have lived in, I keep my states as unsigned
tinyint(1) in the database (boolean true / false if they lived in that
state). This obviously is a weighty translation, so I made a method
in my AppController (now that I think about it, it should be in my
AppModel... but oh well) that will perform the translations for me.
Rather than having this: array( 'region_AL' => 1, 'region_AK' =>
0, ... ), I can have an array that has the state names in it (after
calling AppController::_translateState() ).
I'd suggest just leaving everything machine readable, then translating
it before display. Whether you translate in the controller, model, or
helper, is up to you. I'd recommend making it a helper -- seems the
most Cake-ish way to me.
On Aug 27, 4:26 am, benjamwelker <benjamwel...@gmail.com> wrote:
> You can try creating a couple of methods in your model.
> One afterFind, and another beforeSave.
>
> You can convert the data however you want in the afterFind, and then
> convert it back in the beforeSave so that it doesn't mess up your
> validation and save methods.
>
> But you're going to run into other problems with this method as well.
> The form helper might;' have some issues with the newly formatted
> data, and not knowing exactly what to do with it.
>
> Your best bet is to convert the data how you want right before
> viewing. It's not the easiest to edit a lot of files, but it will
> save you headache in the long run.
>
> Also... to convert a 0/1 to a No/Yes, you can use the built in
> Set::enum($select, $list = null) method, leaving the $list var empty
> will auto populate it with a yes/no array.
>
> echo Set::enum($data['boolean_field']) will output "yes" or "no"
> depending on value. Wrap that in a ucfirst or ucwords to get "Yes"
> "No", done.
>
> On Aug 26, 3:52 pm, ark0n3 <nicolabeg...@gmail.com> wrote:
>
> > That's a good starting point but there's need to change every
> > interested line in the views, while I'd like to obtain an automatic
> > and centralized solution i.e. if I write a new page there should be no
> > need to remember that "work-around"
>
> > On 26 Ago, 15:42, Jon Bennett <jmbenn...@gmail.com> wrote:
>
> > > Hi Nicola,
>
> > > > thanks for your kind reply but that's just what I'd avoid: I'm trying
> > > > to accomplish an automatic way to achieve that result, I know it's not
> > > > right to use a model function and I asked for an MVC and non-
> > > > validation-breaking way..
>
> > > Another way would be to create a helper.
>
> > > How about this helper:http://pastie.org/595351
>
> > > // use it like so
> > > $data = array('Model'=>array('status'=>1));
> > > echo $dataConverter->nice('Model.status', 'onoff', $data);
>
> > > IMHO the thing to remember is that you only adjust the data for
> > > presentation only, hence it's done in either the view or the
> > > controller.
>
> > > hth
>
> > > jon
>
> > > --
>
> > > jon bennett
> > > w:http://www.jben.net/
> > > iChat (AIM): jbendotnet Skype: jon-bennett
--~--~---------~--~----~------------~-------~--~----~
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:
Post a Comment