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

发布于 2024-10-11 17:21:57 字数 2088 浏览 2 评论 0原文


我在使用模块模型类中的方法时遇到问题。
我有一个公共函数,它触发 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();
    }
}

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

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_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();
    }
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-10-18 17:21:57

我猜这与出于不同目的向 $this 对象添加过滤器有关。尝试将其添加到方法的顶部:

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

如果这不起作用,请尝试在 getFirstItem 调用之前回显您的查询,看看它们是否按预期运行:

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

希望有帮助!

谢谢,

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文