5

I have a custom model linked to a custom table. In that table, the column sku is unique. I'd like the following code to always update on duplicate:

Mage::getModel('company/product_log')
  ->setData('sku', 'abc123')
  ->setData('error', 'Invalid price.')
  ->save();

I'd like to do this from within the model itself instead of having to repeat update-vs-insert logic everywhere I want to use this table.

What is the best way to do this, or an existing example already in Magento?

2
  • since you know the sku you want to update, why you dont load the product by it's sku and then call save() on that?
    – kbariotis
    Commented Aug 2, 2014 at 13:44
  • I second @kostaskostarelo. Why do you want to record the duplicate SKU insert attempt? Unless that duplicate-SKU operation is precisely what you're looking for, you should simply check if the SKU exist in the product_log table, and save the model accordingly.
    – laketuna
    Commented Sep 5, 2014 at 18:48

3 Answers 3

4

The method that I came up which seems to work fine is to add this method to my model:

/**
 * @param $sku
 * @return $this|Mage_Core_Model_Abstract
 */
public function loadBySku($sku) {
    $matches = $this->getResourceCollection()
        ->addFieldToFilter('sku', $sku);

    foreach ($matches as $match) {
        return $this->load($match->getId());
    }

    return $this->setData('sku', $sku);
}

So instead of this:

Mage::getModel('company/product_log')
  ->setData('sku', 'abc123')
  ->setData('error', 'Invalid price.')
  ->save();

I now do this:

Mage::getModel('company/product_log')
  ->loadBySku('abc123')
  ->setData('error', 'Invalid price.')
  ->save();

If a row exists with that SKU, it will be loaded, altered and saved. If a row doesn't exist with that SKU, the blank model will have it's SKU set, and then have the error set and the row saved.

loadBySku() could be altered to include more default values when it can't load an existing row by SKU.

2

This will do what you need without reinventing the wheel:

Mage::getModel('company/product_log')
  ->load('abc123', 'sku')
  ->setData('error', 'Invalid price.')
  ->save();

->load() method inherited from Mage_Core_Model_Resource_Db_Abstract

  1. The load will control the unserialization of fields
  2. Performs 1 SQL query
0

Fot example look at app/code/core/Mage/Sales/Model/Order.php how works saving of order_status_history.

public function addStatusHistoryComment($comment, $status = false)
{
    ...
    $history = Mage::getModel('sales/order_status_history')
        ->setStatus($status)
        ->setComment($comment)
        ->setEntityName($this->_historyEntityName);
    $this->addStatusHistory($history);
    return $history;
}

or look at Enterprise_Logging

    return Mage::getSingleton('enterprise_logging/event')->setData(array(
        'ip'         => Mage::helper('core/http')->getRemoteAddr(),
        'user'       => $username,
        'user_id'    => $userId,
        'is_success' => $success,
        'fullaction' => "{$request->getRouteName()}_{$request->getControllerName()}_{$request->getActionName()}",
        'event_code' => $eventCode,
        'action'     => 'login',
    ))->save();

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.