2

次のコードを使用して、category_ids を製品に設定しようとしています。

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

残念ながら、スクリプトは例外で終了します:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'attribute_id' in 'where clause'

いくつかのヒント?$product->setCategoryIds()->save()実行時間がはるかに長いため、使用したくありません。

前もって感謝します。

4

2 に答える 2

1

結局、magento関数の1つを少し変更して、回避策を作成する必要がありました。

/**
* 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;
}

category_idsについての事実を指摘してくれたbixiに感謝します。奇妙なことに、属性ではありませんが、eav_attributeテーブルにcategory_idsというエントリがあり、magentoはこのコードで属性モデルを読み込んでいますが、属性を保存しようとするとクラッシュします。たぶん、category_ids属性を完全に削除したほうがよいので、人々はそれがバグだとは思わないでしょう。

インデックスについて:関数を使用した後の場合に備えて、おそらくすべてのデータのインデックスを再作成する必要があります。使用による製品の節約updateAttributesは9秒から0.75になっているため、大したことではありません。

于 2012-10-15T15:20:42.910 に答える
0

カテゴリを扱う場合は updateAttributes() を使用できません (カテゴリは属性ではありません)。モデル(カタログ/製品)を処理する必要があります...

インデックスの生成 (インデックスが "Update On Saves" になっている場合) と、カタログの構成 (カテゴリはすべて "is_anchor" == true ですか?) に依存するため、低速ですが、必要なものはすべて..

于 2012-10-15T15:09:21.833 に答える