开发者

Magento: use various methods in 1 model

I'm having a problem using methods in my module's model class.

I have a public function which triggers 2 protected methods. The problem is that only the first 1 returns a value.

Here is my class:

<?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.

So, am I doing something wrong?

thanks in advance.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜