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.

    $pieces = Mage::getResourceModel('rad_inventorypieces/piece_collection')
    foreach($pieces as piece)


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()
        ->from($this->getMainTable(), array('sum' => new Zend_Db_Expr('SUM(d)')));

    return $this->getReadConnection()->fetchOne($select)
  • Call to undefined method Rad_Inventorypieces_Model_Resource_Piece_Collection::getReadConnection() – sivakumar Oct 29 '14 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 '14 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 '14 at 10:22
  • Yes, it is not a collection anymore and you can not use addtFieldToFilter method. Use where instead – alex_b Oct 29 '14 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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