Magento:在1个模型中使用各种方法

时间:2011-01-14 13:14:06

标签: magento model


我在模块的模型类中使用方法时遇到问题 我有一个公共函数,触发2个受保护的方法。问题是只有前1个返回一个值 这是我的班级:

<?php
class Osdave_Points_Model_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();
    }
}

现在,如果在_getCustomerRedeeemedPoints()中,我将$this替换为Mage::getResourceModel('points/points_collection'),它可以正常工作。但是,由于我已经在课堂上坚持不懈,我不明白为什么我必须通过法师来实例化:据我所知,$this只能使用一次。
那么,我做错了吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我猜这与将此过滤器的过滤器添加到不同目的有关。尝试将此添加到方法的顶部:

$this->getSelect()->reset();

如果这不起作用,请尝试在getFirstItem来电之前回复您的查询,看看它们是否符合预期:

Mage::log($this->getSelect()."");

希望有所帮助!

谢谢, 乔