0

我有这个错误:

[2012 年 7 月 25 日星期三 15:48:09] [错误] [客户端 50.xxx.xxx.xxx] PHP 致命错误:在 /var/www/magento/app 中的非对象上调用成员函数 getSource() /code/core/Mage/Catalog/Model/Product.php 1389行

在这个函数中:

/**
 * Get attribute text by its code
 *
 * @param $attributeCode Code of the attribute
 * @return string
 */
public function getAttributeText($attributeCode)
{
    return $this->getResource()
       ->getAttribute($attributeCode)
            ->getSource()
                ->getOptionText($this->getData($attributeCode));
}

而且我想在发生错误时记录 $attributeCode 值。所以我把函数改成这样:

public function getAttributeText($attributeCode)
{
    try {
        return $this->getResource()
           ->getAttribute($attributeCode)
                ->getSource()
                    ->getOptionText($this->getData($attributeCode));
    } catch (Exception $e) {
        Mage::log($attributeCode);
        throw($e);
    }
}

…然后重新启动 Apache,但 server.log 中没有显示任何内容。如果我注释掉 throw($e),异常仍然会被抛出。

./lib/Zend/Rest/Server.php 有 set_exception_handler(array($this, "fault"));

但是请告诉我,一旦你在某个地方设置了它,php 就不会忽略所有手动异常处理。因为那会很疯狂。

知道如何仅在发生异常时捕获 $attributeCode 吗?

4

2 回答 2

1

尝试

$attributes = $this->getResource()->getAttribute($attributeCode);
if (!empty($attributes)) {
    return $attributes->getSource()->getOptionText($this->getData($attributeCode));
} else {
    Mage::log($attributeCode);
    throw new InvalidArgumentException('bla bla');
}

// 不知道getAttribute($attributeCode)返回了什么;所以你可以使用 is_null(), isset() 而不是 empty()

于 2012-07-26T22:45:17.803 回答
0

仅出于调试目的,我会这样做:

if (!is_object($this->getResource()->getAttribute($attributeCode)) {
    var_dump($attributeCode);
    exit();
}
于 2012-07-26T21:48:05.067 回答