Re: MVC-way to obtain common data formatting
I think the potential solutions have all been highlighted, but I'd 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 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 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 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
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 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 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
Hmm I see ... well, I think this can only be accomplished with some kind of helper method/class in the view, just like Jon Bennet says. Imho a class like that will get bloated very quickly , because there will be different options to choose from on how to display the data. A small helper for translating model-specific values would be my personal choice. On Wed, Aug 26, 2009 at 3:51 PM, ark0n3 wrote: > > @ Bert: yes that's the problem, I'm looking for a way to accomplish in > an automatic way and not only for boolean fields (which aren't > difficult to implement with an ad-hoc behavior) > > On 26 Ago, 15:46, Bert Van den Brande wrote: >> I don't really understand the problem you present. >> >> Either you display a model field as a read-only piece of information >> in the view, and then there is no problem with data being updated. >> >> Or you display model data in a form that can be edited, and in case of >> for example the boolean you provide a dropdown list or use a radio >> button that displays a readable string to the user but holds the raw >> data as the value. >> >> On Wed, Aug 26, 2009 at 2:46 PM, ark0n3 wrote: >> >> > Hi Jon >> > 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.. >> >> > On 26 Ago, 11:23, Jon Bennett wrote: >> >> Hi Nicola. >> >> >> > I'm trying to understand if and in which way could be possible to >> >> > achieve such a result. Maybe I've explained the wrong way: I need to >> >> > format some common fields like boolean value and achieve this adding >> >> > an afterFind(results) callback in the app_model.php. I'd like to know >> >> > if this is the best way to accomplish this 'cause I noticed that (of >> >> > course) it causes problems when trying to edit something: the datas >> >> > are formatted as I requested in the app_model but this causes >> >> > validation problems (super simple example: boolean humanized as "Yes/ >> >> > No" and no more as 1/0). >> >> >> I would have thought the only time you need to display 1 or 0 as yes >> >> or no is in the view. You're quite right that if you adjust the data >> >> in app_model, it will break your DB. >> >> >> I have a Config value set in my bootstrap, and output that. eg: >> >> >> // bootstrap.php >> >> Configure::write('yesno', array(0=>'No', 1=>'Yes'); >> >> >> // View >> >> Configure::read('yesno.'.$row[$modelClass]['field']); // outputs 'Yes' >> >> for 1, and 'No' for 0. >> >> >> 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
> 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" I think you'll find it easier to tackle this issue once Cakephp 1.3 (I think) is out and data is moved from arrays to objects. Then, you could detect that you're in a view and when you call $model->field it can convert the data for you on the fly. Until then, there is no perfect solution. hth j -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
Hi ark0n3, > 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" I don't see a way around that really. You need to know what the data is, what you want it to be and only need to change it once it's got to the view. You could create a helper with a set of rules which could mangle the data in one hit for you I suppose. j -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
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 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
I don't really understand the problem you present. Either you display a model field as a read-only piece of information in the view, and then there is no problem with data being updated. Or you display model data in a form that can be edited, and in case of for example the boolean you provide a dropdown list or use a radio button that displays a readable string to the user but holds the raw data as the value. On Wed, Aug 26, 2009 at 2:46 PM, ark0n3 wrote: > > Hi Jon > 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.. > > > > On 26 Ago, 11:23, Jon Bennett wrote: >> Hi Nicola. >> >> > I'm trying to understand if and in which way could be possible to >> > achieve such a result. Maybe I've explained the wrong way: I need to >> > format some common fields like boolean value and achieve this adding >> > an afterFind(results) callback in the app_model.php. I'd like to know >> > if this is the best way to accomplish this 'cause I noticed that (of >> > course) it causes problems when trying to edit something: the datas >> > are formatted as I requested in the app_model but this causes >> > validation problems (super simple example: boolean humanized as "Yes/ >> > No" and no more as 1/0). >> >> I would have thought the only time you need to display 1 or 0 as yes >> or no is in the view. You're quite right that if you adjust the data >> in app_model, it will break your DB. >> >> I have a Config value set in my bootstrap, and output that. eg: >> >> // bootstrap.php >> Configure::write('yesno', array(0=>'No', 1=>'Yes'); >> >> // View >> Configure::read('yesno.'.$row[$modelClass]['field']); // outputs 'Yes' >> for 1, and 'No' for 0. >> >> 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
@ Bert: yes that's the problem, I'm looking for a way to accomplish in an automatic way and not only for boolean fields (which aren't difficult to implement with an ad-hoc behavior) On 26 Ago, 15:46, Bert Van den Brande wrote: > I don't really understand the problem you present. > > Either you display a model field as a read-only piece of information > in the view, and then there is no problem with data being updated. > > Or you display model data in a form that can be edited, and in case of > for example the boolean you provide a dropdown list or use a radio > button that displays a readable string to the user but holds the raw > data as the value. > > On Wed, Aug 26, 2009 at 2:46 PM, ark0n3 wrote: > > > Hi Jon > > 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.. > > > On 26 Ago, 11:23, Jon Bennett wrote: > >> Hi Nicola. > > >> > I'm trying to understand if and in which way could be possible to > >> > achieve such a result. Maybe I've explained the wrong way: I need to > >> > format some common fields like boolean value and achieve this adding > >> > an afterFind(results) callback in the app_model.php. I'd like to know > >> > if this is the best way to accomplish this 'cause I noticed that (of > >> > course) it causes problems when trying to edit something: the datas > >> > are formatted as I requested in the app_model but this causes > >> > validation problems (super simple example: boolean humanized as "Yes/ > >> > No" and no more as 1/0). > > >> I would have thought the only time you need to display 1 or 0 as yes > >> or no is in the view. You're quite right that if you adjust the data > >> in app_model, it will break your DB. > > >> I have a Config value set in my bootstrap, and output that. eg: > > >> // bootstrap.php > >> Configure::write('yesno', array(0=>'No', 1=>'Yes'); > > >> // View > >> Configure::read('yesno.'.$row[$modelClass]['field']); // outputs 'Yes' > >> for 1, and 'No' for 0. > > >> 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
Hi Jon 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.. On 26 Ago, 11:23, Jon Bennett wrote: > Hi Nicola. > > > I'm trying to understand if and in which way could be possible to > > achieve such a result. Maybe I've explained the wrong way: I need to > > format some common fields like boolean value and achieve this adding > > an afterFind(results) callback in the app_model.php. I'd like to know > > if this is the best way to accomplish this 'cause I noticed that (of > > course) it causes problems when trying to edit something: the datas > > are formatted as I requested in the app_model but this causes > > validation problems (super simple example: boolean humanized as "Yes/ > > No" and no more as 1/0). > > I would have thought the only time you need to display 1 or 0 as yes > or no is in the view. You're quite right that if you adjust the data > in app_model, it will break your DB. > > I have a Config value set in my bootstrap, and output that. eg: > > // bootstrap.php > Configure::write('yesno', array(0=>'No', 1=>'Yes'); > > // View > Configure::read('yesno.'.$row[$modelClass]['field']); // outputs 'Yes' > for 1, and 'No' for 0. > > 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: MVC-way to obtain common data formatting
Hi Nicola. > I'm trying to understand if and in which way could be possible to > achieve such a result. Maybe I've explained the wrong way: I need to > format some common fields like boolean value and achieve this adding > an afterFind(results) callback in the app_model.php. I'd like to know > if this is the best way to accomplish this 'cause I noticed that (of > course) it causes problems when trying to edit something: the datas > are formatted as I requested in the app_model but this causes > validation problems (super simple example: boolean humanized as "Yes/ > No" and no more as 1/0). I would have thought the only time you need to display 1 or 0 as yes or no is in the view. You're quite right that if you adjust the data in app_model, it will break your DB. I have a Config value set in my bootstrap, and output that. eg: // bootstrap.php Configure::write('yesno', array(0=>'No', 1=>'Yes'); // View Configure::read('yesno.'.$row[$modelClass]['field']); // outputs 'Yes' for 1, and 'No' for 0. 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---