Magento产品加载-loadByAttribute和加载方法之间的差异 [英] Magento product load - difference between loadByAttribute and load methods
问题描述
今天我再次与Magento战斗:),我发现
today I'm fighting with Magento again :) and I found a difference between
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $product_sku);
和
$product = Mage::getModel('catalog/product')->load($product_id);
有人能告诉我这两种方法之间的区别吗?我发现当我通过sku加载产品时,当我尝试用更改的数据重新保存产品时,出现错误异常'Varien_Exception',消息为'无效的方法Varien_Object :: save保存在app \ code \ core \ Mage中\ CatalogInventory \ Model \ Observer.php(153):Varien_Object-> __ call('save',Array)是正确的,因为一旦您尝试通过sku加载,则另一个观察者将产品的库存项目设置为Varien_Object,这看起来像陷阱或我只是不够了解,但是
Can anyone exaplain me a difference between these two approaches? I found that when I'm loading a product by sku then when I 'try to re-save it with changed data then I get error exception 'Varien_Exception' with message 'Invalid method Varien_Object::save in app\code\core\Mage\CatalogInventory\Model\Observer.php(153): Varien_Object->__call('save', Array) that's true because once you try to load by sku then another observer sets product's stock item as Varien_Object, that looks like pitfall or I just dont understand it enough, but
我从一开始就进行Magento的日常开发,因此我对系统了解很多,这对我来说是新的.预先感谢Jaro.
I do daily Magento development from its beginnig so I know a lot about system and this is new for me. Thanks in advance, Jaro.
推荐答案
有趣.虽然这两种方法都会为您提供一个具有完全加载的EAV数据的单个产品模型实例(前提是未传递或不提供loadByAttribute()
的第三个参数),但是添加库存相关数据的观察者对于产品与产品而言是不同的集合,但两个库存信息对象都已添加到产品数据键"stock_item".这值得商bat,但这感觉像是个错误.我认为Mage_CatalogInventory_Model_Observer::saveInventoryData()
或Mage_CatalogInventory_Model_Observer::_prepareItemForSave()
可以解决这个问题.
Interesting. While both methods will net you a single product model instance with fully loaded EAV data (provided the third parameter of loadByAttribute()
is not passed or is *
), the observers which add stock-related data are different for products vs. product collections, yet both stock information objects are added to the product data key "stock_item". It's debatable, but this feels like a bug. I would think that Mage_CatalogInventory_Model_Observer::saveInventoryData()
or Mage_CatalogInventory_Model_Observer::_prepareItemForSave()
would handle this.
您可以通过使用stock_item
对象在您的产品实例上完全设置产品库存项目来解决此问题.
You could resolve this issue by setting the product stock item fully on your product instance using the stock_item
object.
这篇关于Magento产品加载-loadByAttribute和加载方法之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!