in the Product model:
--------
function getTotal($id) {
return Set::extract($this->find('all',
array('conditions'=>array(
'Product.order_id'=>$id
),
'fields'=>array(
'SUM(id)
AS total'
)
)
), '/0/0/total');
}
-------
in the Orders Controller
$this->Order->Product->getTotal($orderId);
To make your life easier getting started with cake, it's best to
"unlearn" rails or java habits.
I'm not saying that one approach is somehow better than the other,
they are just different...
Some expectations you have from rails, simply will not be met in cake,
but that's not to say that it will detract from extremely fast
development and time to deployment and quality of an application. Just
gotta learn them things to do it cake-like ;)
On Oct 9, 3:29 am, cbankier <[EMAIL PROTECTED]> wrote:
> Hi,
> I'm new to Cake, but have used rails quite a bit as well as Java
> frameworks etc.
>
> Is it possible for a model to have instance methods? eg have behaviour
> that depends on the objects state - the fundamental concept of
> objects.
>
> Here is an example of something I might do in rails/Java. The syntax
> is almost certainly wrong for cake, but hopefully you can see what I'm
> trying do to and hopefully explain the cake way of achieving this.
>
> Secondly in this example the model is aware if it's associations. Is
> that possible/recommended in Cake?
>
> class Order extends AppModel {
> var $hasMany = 'Product';
>
> function getTotal(){
> $total = 0;
> foreach($this->Product->find('all') as $product):
> $total += $product->price;
> endforeach;
>
> return $total;
>
> }
> }
>
> and then a controller might do:
>
> $order = $this->Order->findById(5);
> $orderTotal = $order->getTotal();
>
> Any help appreciated.
> Cheers,
> Colin.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---