title: Update products programmatically in Magento
body: <p>I'm working on a script that will create or update products in my catalog.<br>
  The script works fine when the product needs to be created, but it fails when the product already exists in the database giving me (many times) the following messages :  </p>
  
  <blockquote>
    <p>2011-09-30T08:00:53+00:00 ERR (3): Recoverable Error: Argument 3
    passed to
    Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract::_canUpdateAttribute()
    must be an array, null given, called in ...<br>
    2011-09-30T08:00:53+00:00
    ERR (3): Recoverable Error: Argument 3 passed to
    Mage_Eav_Model_Entity_Abstract::_canUpdateAttribute() must be an
    array, null given, called in ...<br>
    2011-09-30T08:00:53+00:00 ERR (3):
    Warning: array_key_exists() [function.array-key-exists]: The
    second argument should be either an array or an object  in ...</p>
  </blockquote>
  
  <p>I've been looking at the method quoted in the message, but I can't find any reason why the script fails.<br>
  The script first try to load a product using :  </p>
  
  <p><code>$product = Mage::getModel('catalog/product')-&gt;loadByAttribute('sku', $sku);</code>  </p>
  
  <p>and then test if the product was retrieved using a simple <code>if(!$product) { //creation }</code>.<br>
  All the code that follow the if statement is shared for creation or update and consists of setter calls on product object.</p>
  
  <p>Here is the code I use :</p>
  
  <pre><code>$product = Mage::getModel('catalog/product')-&gt;loadByAttribute('sku', $sku);
  if(!$product) {
      // the product doesn't exist yet
      $product = new Mage_Catalog_Model_Product();
      $product-&gt;setSku($sku);
      $product-&gt;setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
      $product-&gt;setCreatedAt(strtotime('now'));
  }
  // setters calls
  $product-&gt;setTeinte(trim((string)$record-&gt;web_teinte));
  // ...
  // finally save the product
  $product-&gt;save();
  </code></pre>
  
  <p>Maybe someone has already faced the same problem.<br>
  Any help is welcome ! Thank you.</p>