Magento何时使用Mage::getResourceModel和Mage::getModel
我刚刚开始使用Magento,特别是关于模型和ORM的工作原理 我用过这三种方法Magento何时使用Mage::getResourceModel和Mage::getModel,magento,Magento,我刚刚开始使用Magento,特别是关于模型和ORM的工作原理 我用过这三种方法 Mage::getResourceModel() Mage::getModel() Mage::getSingleton() 谁能告诉我它们之间的区别是什么 我发现getSingleton()共享内存,而getModel()为加载的同一个表的新对象使用新内存 我已经使用了上述所有方法,但无法区分它们以及何时适合使用哪种方法。完美不同于getsingleton和getmodel的示例。 Mage::getSingl
Mage::getResourceModel()
Mage::getModel()
Mage::getSingleton()
谁能告诉我它们之间的区别是什么
我发现getSingleton()
共享内存,而getModel()
为加载的同一个表的新对象使用新内存
我已经使用了上述所有方法,但无法区分它们以及何时适合使用哪种方法。完美不同于getsingleton和getmodel的示例。 Mage::getSingleton() Mage::getSingleton()将首先检查内存中是否存在相同的类实例。如果实例存在,那么它将从内存返回相同的对象。因此,Mage::getSingleton()比Mage::getModel()更快 范例
$product1 = Mage::getSingleton('catalog/product');
$product2 = Mage::getSingleton('catalog/product');
$product1 = Mage::getModel('catalog/product');
$product2 = Mage::getModel('catalog/product');
$product1和$product2都将共享操作系统的相同内存,并且每次只返回一个实例
Mage::getModel()
每次配置中存在对象时,Mage::getModel()都会创建一个新的对象实例
范例
$product1 = Mage::getSingleton('catalog/product');
$product2 = Mage::getSingleton('catalog/product');
$product1 = Mage::getModel('catalog/product');
$product2 = Mage::getModel('catalog/product');
$product1和$product2都具有相同对象的不同瞬间,并且占用不同的内存
Mage::getResourceModel()
据我所知,Magento中的所有集合都是资源模型。它们由
Mage::getResourceModel()
或
你使用哪种功能其实并不重要;后一个简单地称为第一个。Magento团队只是选择将集合作为资源的一部分,可能是因为集合需要经常查询数据库。通常,除了集合之外,您不必调用Mage::getResourceModel()
balajimca的好文章同样,如果您知道需要哪些属性,那么使用带有过滤器的Mage::getResourceModel在速度和内存方面都比通过Mage::getModel(“目录/产品”)加载效率高5倍 e、 g。 从数据库检索产品的getResourceModel方法
$collection = Mage::getResourceModel('catalog/product_collection')
->addFieldToFilter('entity_id', array($productId))
->addAttributeToSelect(array('name'))
->setPageSize(1);
$product = $collection->getFirstItem();
加载模型(这将加载所有eav属性)
[详情请参阅][1]
上帝保佑你的灵魂。如何使用类别id在observer中调用选定的产品类别@SamThis很有帮助,但我必须在扩展
Mage\u Core\u Model\u Resource\u Db\u Collection\u Abstract的非基于产品的具体类上使用->addFieldToSelect([])是的,对于非EAV集合,需要使用addFieldToSelect([])