且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento getSingleton()vs getModel()问题

更新时间:2023-11-30 10:18:52

Mage :: getModel()将始终为给定模型返回一个新的Object:

Mage::getModel() will always return a new Object for the given model:

/**
 * Retrieve model object
 *
 * @link    Mage_Core_Model_Config::getModelInstance
 * @param   string $modelClass
 * @param   array|object $arguments
 * @return  Mage_Core_Model_Abstract|false
 */
public static function getModel($modelClass = '', $arguments = array())
{
    return self::getConfig()->getModelInstance($modelClass, $arguments);
}

Mage :: getSingleton()将检查给定模型的Object是否已经存在,如果存在,则返回该对象.如果不存在,它将创建给定模型的新对象,并将其存在的注册表中.下次调用不会返回新对象,而是返回现有对象:

Mage::getSingleton() will check whether the Object of the given model already exists and return that if it does. If it doesn't exist, it will create a new object of the given model and put in registry that it already exists. Next call will not return a new object but the existing one:

/**
 * Retrieve model object singleton
 *
 * @param   string $modelClass
 * @param   array $arguments
 * @return  Mage_Core_Model_Abstract
 */
public static function getSingleton($modelClass='', array $arguments=array())
{
    $registryKey = '_singleton/'.$modelClass;
    if (!self::registry($registryKey)) {
        self::register($registryKey, self::getModel($modelClass, $arguments));
    }
    return self::registry($registryKey);
}

由于每种产品都是唯一的,因此您总是需要一个全新的Product对象/模型...

In your case you always want a completely new Product object/model since every product is unique...