Re: best way to do calculated fields in afterFind

2008-01-15 Thread b logica

On Jan 15, 2008 2:47 PM, Tim W <[EMAIL PROTECTED]> wrote:
>
> Hi all,
>
> Is this the best way to create a calculated field in a model object?
> It works perfectly, but having to merge an array doesn't seem ideal,
> and it's not OO at all.
>
> function afterFind($results)
> {
> for ($i=0; $i < count($results); $i++) {
> $lens = $results[$i]['Lens'];
> $description = $lens['wide_zoom'] . '-' .
> $lens['long_zoom'];
> $lens = array_merge($lens, array('description' =>
> $description));
> $results[$i]['Lens'] = $lens;
> }
>
> return $results;
> }
>

You can avoid creating the tmp var and merging by adding a new key to
the array directly:

$results[$i]['Lens']['description'] = $results[$i]['Lens']['wide_zoom']
. '-'
. $results[$i]['Lens']['long_zoom'];

Or concatenate the two fields with SQL as a third column.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



best way to do calculated fields in afterFind

2008-01-15 Thread Tim W

Hi all,

Is this the best way to create a calculated field in a model object?
It works perfectly, but having to merge an array doesn't seem ideal,
and it's not OO at all.

function afterFind($results)
{
for ($i=0; $i < count($results); $i++) {
$lens = $results[$i]['Lens'];
$description = $lens['wide_zoom'] . '-' .
$lens['long_zoom'];
$lens = array_merge($lens, array('description' =>
$description));
$results[$i]['Lens'] = $lens;
}

return $results;
}

Many thanks

Tim
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---