1

I have custom model with 4 fields(a,b,c,d). I need sum of "d" field. So my solution using collection loop the d values and make the sum.For this is any simple solution? Thanks in advance.

  $available=0;
    $pieces = Mage::getResourceModel('rad_inventorypieces/piece_collection')
    foreach($pieces as piece)
    {

$available=+$piece->getAvailable();
    }

1 Answer 1

2

It is better to implement new method in class of rad_inventorypieces/piece resource model

Method might look like this:

/**
 * @return string
 */
public function getSumD()
{
    $select = $this->getReadConnection()
        ->select()
        ->from($this->getMainTable(), array('sum' => new Zend_Db_Expr('SUM(d)')));

    return $this->getReadConnection()->fetchOne($select)
}
4
  • Call to undefined method Rad_Inventorypieces_Model_Resource_Piece_Collection::getReadConnection()
    – sivakumar
    Oct 29, 2014 at 10:11
  • You need to implement it not in collection model, but in resource model Rad_Inventorypieces_Model_Resource_Piece
    – alex_b
    Oct 29, 2014 at 10:14
  • its working.on that i need to add filter.now addtfieldtofilter undefined method error.So can i add where clause
    – sivakumar
    Oct 29, 2014 at 10:22
  • Yes, it is not a collection anymore and you can not use addtFieldToFilter method. Use where instead
    – alex_b
    Oct 29, 2014 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.