2012-08-24 37 views
0

我正在在我從我的自定義選項卡上載文件如下「文件沒有上傳」 Magento的錯誤,即使文件被上傳到特定的文件夾

enter image description here

我創建了一個觀察者的事件相關的應用稱爲catalog_product_save_after。在特定的功能,我上傳這些文件到目錄/產品文件夾,並將其上傳到這些文件夾沒有任何錯誤。

但是,當我試圖將這些圖像分配給該特定產品時,出現以下錯誤。 File was not uploaded在文件E:\xampp\htdocs\magento\lib\Varien\File\Uploader.php on line 152

如果我檢查system.log然後我看到2012-08-24T11:33:15+00:00 ERR (3): User Error: Some transactions have not been committed or rolled back in E:\xampp\htdocs\magento\lib\Varien\Db\Adapter\Pdo\Mysql.php on line 3645此錯誤。

我的觀察功能如下

public function Savedata($observer) 
    { 
     $params  = Mage::app()->getRequest()->getParams(); 

     $product = $observer->getEvent()->getProduct(); 

     $product_id = $product->getId(); 


     $filesData = array(); 


     $keysData = array_keys($_FILES); 


     foreach($keysData as $keys) 
     { 
      $uploader = new Varien_File_Uploader("$keys"); 
      $uploader->setAllowedExtensions(array('jpeg', 'jpg', 'png', 'tiff')); 
      $uploader->setAllowRenameFiles(true); 
      $uploader->setFilesDispersion(false); 

      $path = Mage::getBaseDir('media') . DS . "catalog" . DS . "product"; 

      if(!is_dir($path)) 
      { 
       mkdir($path); 
      } 

      $extension = pathinfo($_FILES["$keys"]['name'], PATHINFO_EXTENSION); 

      $date = new DateTime(); 

      $file_name     = $keys . "_" . $product_id . "." . $extension; 

      $_FILES["$keys"]['name'] = $file_name; 

      $uploader->save($path, $_FILES["$keys"]['name']); 
      $filesData[] = $path .DS. $_FILES["$keys"]['name']; 
     } 

     print_r($filesData); 
     ///till this works file.. when i add following code, program dies. 

     try 
     { 
      $productData = Mage::getModel('catalog/product')->load($product_id); 

      print_r($productData->getData()); 


      foreach($filesData as $file) 
      { 
       $productData->addImageToMediaGallery($file, "image" ,false, false); 
      } 
      $productData->save(); 

      $productData = Mage::getModel('catalog/product')->load($product_id); 

      print_r($productData->getData()); 
     } 
     catch (Exception $e) 
     { 
      echo $e->getMessage() . "||" . $e->getCode() . "||" . $e->getFile() . "||" . $e->getLine(); 
     } 


     exit(); 
    } 

我要去的地方錯了什麼建議嗎?爲什麼會出現此錯誤?

我使用Magento的1.7

回答

1

我看你要去當你保存它以更新目錄/產品對象。

您在使用事件catalog_product_save_after

觀察者內,您撥打:

$productData = Mage::getModel('catalog/product')->load($product_id); 
... 
$productData->save(); 

$ productData是目錄/產品對象 - >保存它。

你可以猜到會發生什麼,它會觸發事件catalog_product_save_after等等,永遠循環。

我看到你打電話給$productData->save();,因爲在這個事件中,這個值已經保存了,不能改變。

因此,不使用catalog_product_save_after,您可以使用catalog_product_save_before

與您的代碼相同,但刪除$productData->save();因爲它會自動調用保存。

Mage_Core_Model_Abstract 

public function save() 
{ 
    /** 
    * Direct deleted items to delete method 
    */ 
    if ($this->isDeleted()) { 
     return $this->delete(); 
    } 
    if (!$this->_hasModelChanged()) { 
     return $this; 
    } 
    $this->_getResource()->beginTransaction(); 
    $dataCommited = false; 
    try { 
     $this->_beforeSave(); 
     if ($this->_dataSaveAllowed) { 
      //see this line below, it will call save, so you don't need to call save in your `catalog_product_save_before` (no looping forever) 
      $this->_getResource()->save($this); 
      $this->_afterSave(); 
     } 
     $this->_getResource()->addCommitCallback(array($this, 'afterCommitCallback')) 
      ->commit(); 
     $this->_hasDataChanges = false; 
     $dataCommited = true; 
    } catch (Exception $e) { 
     $this->_getResource()->rollBack(); 
     $this->_hasDataChanges = true; 
     throw $e; 
    } 
    if ($dataCommited) { 
     $this->_afterSaveCommit(); 
    } 
    return $this; 
} 
+0

是的..我在想同樣的..非常好的解釋。非常感謝。 – KuKu

相關問題