Magento: use various methods in 1 model
<?php
class Osdave_Points_Mod开发者_运维知识库el_Mysql4_Points_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
const POINTS_CONFIRMED = 2;
const POINTS_REDEEMED = 4;
protected $_customer;
public function _construct()
{
parent::_construct();
$this->_init('points/points');
$this->_customer = Mage::getSingleton('customer/session')->getCustomer();
}
public function getCustomerAvailablePoints()
{
$confirmed = $this->_getCustomerConfirmedPoints();
$redeemed = $this->_getCustomerRedeeemedPoints();
$balance = ($confirmed - $redeemed);
return $balance;
}
protected function _getCustomerConfirmedPoints()
{
$availablePoints = $this->addFieldToFilter('customer_id', $this->_customer->getId())
->addFieldToFilter('points_status', self::POINTS_CONFIRMED)
->addFieldToSelect('points_pending')
->addExpressionFieldToSelect('available_points', 'SUM({{points_pending}})', 'points_pending');
return $availablePoints->getFirstItem()->getAvailablePoints();
}
protected function _getCustomerRedeeemedPoints()
{
$redeemedPoints = $this->addFieldToFilter('customer_id', $this->_customer->getId())
->addFieldToFilter('points_status', self::POINTS_REDEEMED)
->addFieldToSelect('points_pending')
->addExpressionFieldToSelect('redeemed_points', 'SUM({{points_pending}})', 'points_pending');
return $redeemedPoints->getFirstItem()->getRedeemedPoints();
}
}
Now, if, in _getCustomerRedeeemedPoints(), I replace $this
by Mage::getResourceModel('points/points_collection')
it works fine. But as I already am insdide the class, I don't understand why I have to instance it through Mage: as far as I understand, $this
is only available once.
I'm guessing this has to do with adding filters to the $this object for different purposes. Try adding this to the top of your methods:
$this->getSelect()->reset();
If that doesn't work, try echoing your queries before your getFirstItem
calls and see if they behave as expected:
Mage::log($this->getSelect()."");
Hope that helps!
Thanks, Joe
精彩评论