3

Can anyone know what is the difference between use of

$product->getResource()->save($product);

$product->save();

1 Answer 1

3

There is not a huge difference. When calling save directly from the product object, that function also calls $product->getResource()->save($product).

However, both the product model and product resource model can have beforeSave() and afterSave() events. So when you call $product->getResource()->save($product), you end up skipping the beforeSave/afterSave events that exist within the main product model and only those that exist within the resource model are called.

The main difference between the two, is that when calling $product->save(), it calls Mage_Catalog_Model_Product::_beforeSave() which contains this logic:

/**
 * Check product options and type options and save them, too
 */
protected function _beforeSave()
{
    $this->cleanCache();
    $this->setTypeHasOptions(false);
    $this->setTypeHasRequiredOptions(false);

    $this->getTypeInstance(true)->beforeSave($this);

    $hasOptions         = false;
    $hasRequiredOptions = false;

    /**
     * $this->_canAffectOptions - set by type instance only
     * $this->getCanSaveCustomOptions() - set either in controller when "Custom Options" ajax tab is loaded,
     * or in type instance as well
     */
    $this->canAffectOptions($this->_canAffectOptions && $this->getCanSaveCustomOptions());
    if ($this->getCanSaveCustomOptions()) {
        $options = $this->getProductOptions();
        if (is_array($options)) {
            $this->setIsCustomOptionChanged(true);
            foreach ($this->getProductOptions() as $option) {
                $this->getOptionInstance()->addOption($option);
                if ((!isset($option['is_delete'])) || $option['is_delete'] != '1') {
                    $hasOptions = true;
                }
            }
            foreach ($this->getOptionInstance()->getOptions() as $option) {
                if ($option['is_require'] == '1') {
                    $hasRequiredOptions = true;
                    break;
                }
            }
        }
    }

After this runs, the same $this->getResource()->save($this) is called.

6
  • in my custom extention when i use $product->save(); it give me error and when i use other its works fine. Commented Oct 3, 2016 at 3:42
  • what's the error? and in your custom extension, is your product of the class Mage_Catalog_Model_Product or is it using your custom class? Commented Oct 3, 2016 at 3:43
  • i create product programatically with use of my custom extension. when i change its qty and save product it gives error. so what is the main diffrent between those two method? Commented Oct 3, 2016 at 3:45
  • You need to tell me the exact error you are getting. Otherwise, it could be a million different things. Commented Oct 3, 2016 at 3:46
  • can you give me exact diffrent between those two? Commented Oct 3, 2016 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.