Magento Mage getResourceModel, Mage getModel 和 Mage getSingleton() 的区别 when to use Mage getResourceModel, Mage getModel and Mage getSingleton()
getSingleton 和getModel的
Mage::getSingleton()
Mage::getSingleton() 将首先检查内存中是否存在相同的类实例. 如果实例存在,那么它将从内存中返回相同的对象.所以Mage::getSingleton() 比Mage::getModel()快.
$product1 = Mage::getSingleton('catalog/product');
$product2 = Mage::getSingleton('catalog/product');
$product1
和$product2
都将共享相同的OS内存,并且每次只返回一个实例。
Mage::getModel()
Mage::getModel()会在每次配置中存在这样的对象时创建对象的新实例。
$product1 = Mage::getModel('catalog/product');
$product2 = Mage::getModel('catalog/product');
$product1
和$product2
都具有相同对象的不同时刻,并占用不同的内存
Mage::getResourceModel();
据我所知,Magento中的所有集合都是资源模型。它们要么被实例化
Mage::getResourceModel()
要么
Mage::getModel()->getCollection()
说白了就是:Mage::getResourceModel('module/model_collection')
相当于 Mage::getModel('module/model')->getCollection())
使用哪个函数并不重要; 后者简单地称为第一个。Magento团队只是选择将集合作为资源的一部分,可能是因为集合需要查询数据库。通常情况下,你不需要Mage::getResourceModel()
除了集合以外的任何其他东西。
发表评论
沙发空缺中,还不快抢~