Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Magento中的updateAttributes为产品分配类别ID_Php_Magento_Product - Fatal编程技术网

Php 使用Magento中的updateAttributes为产品分配类别ID

Php 使用Magento中的updateAttributes为产品分配类别ID,php,magento,product,Php,Magento,Product,我正在尝试使用以下代码将category_ID设置为产品: <?php Mage::getSingleton('catalog/product_action')->updateAttributes( array($product->getId()), array("category_ids"=>$this->convertCategories($prod['categories'])) ,0 ); ?> 不幸的是,脚本退出时出

我正在尝试使用以下代码将category_ID设置为产品:

<?php
Mage::getSingleton('catalog/product_action')->updateAttributes(
    array($product->getId()), 
    array("category_ids"=>$this->convertCategories($prod['categories']))
    ,0
);
?> 

不幸的是,脚本退出时出现异常:
SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“attribute\u id”

一些提示?我不想使用
$product->setCategoryIds()->save()
,因为它的执行时间要长得多


提前感谢。

在处理类别(类别不是属性)时,不能使用updateAttributes()。 您必须处理模型(目录/产品)

由于索引生成(如果您的索引处于“保存时更新”)和目录配置(您的类别是否全部为“is_anchor”==true?)的关系,速度较慢
所有这些都是缓慢但必要的

最后,我不得不稍微修改magento函数中的一个,并制定一个解决方案:

/**
* Updates product categories
*
* @param Mage_Catalog_Model_Product $product
* @param array $categoryIds
* @return MagentoImporter
*/
protected function updateCategories(&$product, $categoryIds)
{
    /** @var Varien_Db_Adapter_Pdo_Mysql **/
    $dbw = Mage::getSingleton('core/resource')->getConnection('core_write');
    $productCategoryTable = Mage::getSingleton('core/resource')->getTableName('catalog/category_product');

    $oldCategoryIds = $product->getCategoryIds();

    $insert = array_diff($categoryIds, $oldCategoryIds);
    $delete = array_diff($oldCategoryIds, $categoryIds);

    if (!empty($insert)) {
        $data = array();
        foreach ($insert as $categoryId) {
            if (empty($categoryId)) {
                continue;
            }
            $data[] = array(
                'category_id' => (int)$categoryId,
                'product_id'  => (int)$product->getId(),
                'position'    => 1
            );
        }
        if ($data) {
            $ris = $dbw->insertMultiple($productCategoryTable, $data);
        }
    }
    if (!empty($delete)) {
        foreach ($delete as $categoryId) {
            $where = array(
                'product_id = ?'  => (int)$product->getId(),
                'category_id = ?' => (int)$categoryId,
            );
            $ris = $dbw->delete($productCategoryTable, $where);
        }
    }
    return $this;
}
感谢bixi指出了category_ID的事实,奇怪的是,尽管它不是一个属性,但eav_属性表中有一个名为category_ID的条目,magento正在用此代码加载属性模型,但在试图保存属性时崩溃了。也许最好删除category_id属性,这样人们就不会认为这是一个bug


关于索引:可能我需要重新编制所有数据的索引,以防万一在使用我的函数之后,这并不是什么大问题,因为使用
updateAttributes
保存产品的时间从9秒增加到了0.75。

是的,使用您的代码,您将没有选择:您需要手动刷新索引“category\u flat”、“category\u product”或者“catalog\u url\u rewrite”,如果您的产品的url包含类别名称。使用Magento 1.8.1可以像符咒一样工作,节省大量时间!谢谢!