How to overwrite database fields in a model?

2009-07-21 Thread Simon

Dear All,

I'm a beginner with CakePHP (and therefore new to this group) and I'm
currently facing a (basic) problem:

I have a database table with some information like the mobile phone,
status of a shipment (which is a TYNINT field in my mysql db), etc. I
want to know if there is a simple way to parse the value of the fields
directly in the model in order to have a more readable results in my
views. Here is a short example to be clear:

- My status fields has the following value in my db table: 0, 1, 2, 3
- my business rule is the following:
 - 0 = not yet processed
 - 1 = processing
 - 2 = shipped
 - 3 = delivered

How can change my model to include this business logic directly
there ? I don't want to parse the results in every views, because if
the logic change I will have to change it in every view.

Thanks in advance for your help, cheers, Simon

--~--~-~--~~~---~--~~
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: How to overwrite database fields in a model?

2009-07-21 Thread Mark

i guess i will have to show you my little secret - how it can be
achieved in a very neat way

in your Model:

/** Static Enums **/

/**
 * Static Model::method()
 * BILL STATUS
 */
function statuses($value = null) {
$options = array(
BILL_STATUS_NEW = __('New',true),
BILL_STATUS_OPEN = __('Open',true),
BILL_STATUS_CLOSED = __('Closed',true),
);

if ($value !== null) {
if (array_key_exists($value, $options)) {
return $options[$value];
}
return '';
}
return $options;
}

now, in your views, controller etc you can simply use it statically:

echo $form-input('status', array('options'=CompanyBill::statuses
()));

or in the controller

$this-flashMessage('Status: '.CompanyBill::statuses($value));
= Closed, e.g.

PS: instead of defining constants at the top of your model you could
as well use the numeric values right away (but the constant way is
cleaner, especially if you need some logic in the controller like

if ($value == STATUS_PROCESSED) {}

thats more readable then
if ($value == 1) {}


PPS: remember that the model needs to be included if you want to use
those enums in other controllers
but from my experience they are always included anyway through some
HasMany/BelongsTo relation.

happy baking :)


On 21 Jul., 23:30, Simon simon.gani...@gmail.com wrote:
 Dear All,

 I'm a beginner with CakePHP (and therefore new to this group) and I'm
 currently facing a (basic) problem:

 I have a database table with some information like the mobile phone,
 status of a shipment (which is a TYNINT field in my mysql db), etc. I
 want to know if there is a simple way to parse the value of the fields
 directly in the model in order to have a more readable results in my
 views. Here is a short example to be clear:

 - My status fields has the following value in my db table: 0, 1, 2, 3
 - my business rule is the following:
  - 0 = not yet processed
  - 1 = processing
  - 2 = shipped
  - 3 = delivered

 How can change my model to include this business logic directly
 there ? I don't want to parse the results in every views, because if
 the logic change I will have to change it in every view.

 Thanks in advance for your help, cheers, Simon
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---