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
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:
On Fri, Apr 20, 2012 at 2:27 AM, jasonix <jayhina...@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
> 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
Try: SELECT SUM(customerpurchases.amount) AS total From customerpurchases WHERE customerbill_id = $billid
This can be written as a find(): $total = $this->CustomerPurchases->find('first', array('conditions'=>array('customerbill_id'=>$billid), 'fields'=>array('sum(customerpurchases.amount) as total'));
If you then do a debug($total) you will get something like:
> Now, how can i extract or get the [$total] value in the array and > store it as an integer/float variable? Like $extractedTotal = 300.00.
> Thanks so much. =)
> -- > 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
> [...] > This can be written as a find(): > $total = $this->CustomerPurchases->find('first', > array('conditions'=>array('customerbill_id'=>$billid), > 'fields'=>array('sum(customerpurchases.amount) as total'));
On Apr 24, 2:48 am, 0x20h <k...@informatik.uni-marburg.de> wrote:
> Am 22.04.2012 20:24, schrieb kdubya:
> > [...]
> > This can be written as a find():
> > $total = $this->CustomerPurchases->find('first',
> > array('conditions'=>array('customerbill_id'=>$billid),
> > 'fields'=>array('sum(customerpurchases.amount) as total'));
> On Apr 24, 2:48 am, 0x20h <k...@informatik.uni-marburg.de> wrote:
>> Am 22.04.2012 20:24, schrieb kdubya:
>>> [...]
>>> This can be written as a find():
>>> $total = $this->CustomerPurchases->find('first',
>>> array('conditions'=>array('customerbill_id'=>$billid),
>>> 'fields'=>array('sum(customerpurchases.amount) as total'));
> yes, but it still returns an array... what i want is to return a float
> value..
> -- > 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
On Wed, Apr 25, 2012 at 1:03 PM, jasonix <jayhina...@gmail.com> wrote:
> On Apr 24, 2:48 am, 0x20h <k...@informatik.uni-marburg.de> wrote: > > Am 22.04.2012 20:24, schrieb kdubya:
> > > [...] > > > This can be written as a find(): > > > $total = $this->CustomerPurchases->find('first', > > > array('conditions'=>array('customerbill_id'=>$billid), > > > 'fields'=>array('sum(customerpurchases.amount) as total'));
> yes, but it still returns an array... what i want is to return a float > value..
> -- > 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