“文件未上传”magento错误甚至文件上传到特定文件夹

时间:2012-08-24 11:56:53

标签: magento file-upload

我正在制作一个应用程序,我从我的自定义标签中上传文件,如下所示

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 个答案:

答案 0 :(得分: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();,因为它会自动调用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;
}