sql的方式修改产品属性 - 避免使用event (magento)

来源:互联网 发布:元数据定义都用大写吗 编辑:程序博客网 时间:2024/05/29 08:58

调用:

$this->updateAttribute($product_id,"1","is_edit","catalog_product_entity_text");

其中最后一个参数:catalog_product_entity_text,要注意,这个是这个属性的类型对应的表。

//保存产品的is_edit属性

   

  private function updateAttribute($id,$values,$attribute,$tablename){                $connection        = $this->getConnection('core_write');                $newvalues        = $values;                $productId        = $id;                $attributeId    = $this->getAttributeId($attribute);                if($this->checkIfIdExists($productId)){                    $sql = "UPDATE " . $this->getTableName($tablename) . " cped                            SET  cped.value = ?                        WHERE  cped.attribute_id = ?                        AND cped.entity_id = ?";                    $connection->query($sql, array($newvalues, $attributeId, $productId));                    return 1;                }else{                    return 0;                }                    }            private function checkIfIdExists($id){        $connection = $this->getConnection('core_read');        $sql        = "SELECT COUNT(*) AS count_no FROM " . $this->getTableName('catalog_product_entity') . "    WHERE entity_id = ?";        $count        = $connection->fetchOne($sql, array($id));        if($count > 0){            return true;        }else{            return false;        }    }            private function getTableName($tableName){        return Mage::getSingleton('core/resource')->getTableName($tableName);    }        private function getAttributeId($attribute_code){        $connection = $this->getConnection('core_read');        $sql = "SELECT attribute_id                    FROM " .$this->getTableName('eav_attribute') . "                WHERE                    entity_type_id = ?                    AND attribute_code = ?";        $entity_type_id = $this->getEntityTypeId();        return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));    }    private function getConnection($type = 'core_read'){        return Mage::getSingleton('core/resource')->getConnection($type);    }    private function getEntityTypeId($entity_type_code = 'catalog_product'){        $connection = $this->getConnection('core_read');        $sql        = "SELECT entity_type_id FROM " . $this->getTableName('eav_entity_type') . " WHERE entity_type_code = ?";        return $connection->fetchOne($sql, array($entity_type_code));    }


0 0
原创粉丝点击