Saturday, April 21, 2012

Re: SQL sum function to store as single variable

Because flash expects a string.

debug($total);
$this->log($total);

If you're going to use query() for this I think you'd be better off
calling stored procedures. Or, put another way, I think you'd be
better off using stored procedures.

That is, if your model's logic doesn't also include some need to loop
over the Customerpurchase data and do other stuff. If you do, then use
a regular find() and sum your total in afterFind() so you have
something like this:

array(
'Customerpurchase' => array(
0 => array( ... ),
1 => array( ... ),
...
),
'total' => $the_total
)



On Fri, Apr 20, 2012 at 2:27 AM, jasonix <jayhinanay@gmail.com> wrote:
> hi, i'm newby in using cakePHP.
>
> i have 2 tables: customerbills and customerpurchases.
>
> what i want to do is to make a sum query in customerpurchases and
> update it in customerbills.
> customerpurchases fields:
> -id
> -customerbill_id
> -product
> -amount
>
> customerbills fields:
> -id
> -customer_id
> -amountbill
>
> Here's what i did in my customerpurchasesController:
>
> $billid = 1;
>
> $total = $this->Customerpurchase->query("SELECT
> SUM(customerpurchases.amount) AS total From customerpurchases Group by
> customerbill_id = $billid");
>
> now i try this for updating:
> $this->Customerpurchase->query("UPDATE bills SET amountbill = $total
> Where id = $billid");
>
> but i it doesn't work. I tried to display the variable $total in
> session flash to check what's inside the variable and it displays
> "arrays" ..
>
> pls. help me. Thanks..
>
> --
> Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
>
>
> 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

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


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

No comments: