Magento - 如何运行自定义产品属性脚本

4
我有一个关于Magento属性的问题。我创建了一个自定义产品输入文本属性,用于保存整数数据类型,但是Magento将其存储为varchar。我在stackoverflow上询问了此问题,他们告诉我没有办法将产品属性类型从字符串更改为整数。
所以我的解决方案是创建一个自定义整数产品属性。我在Google上搜索了几天,找到了一篇文章,其中提供了一个可以创建自定义属性的脚本http://magentotutorialbeginners.blogspot.com/2014/03/create-product-attribute.html?showComment=1442885130592#c2319234413343201281
问题是我不知道如何运行或使用它。
问题:
这个脚本怎么运行?您能给我一个逐步指南吗?
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'custom_mprice', array( 
        'input' => 'text',
        'type' => 'int',
        'label' => 'Enter Max Price',
        'backend' => '',
        'visible' => 1,
        'required' => 0,
        'user_defined' => 1,
        'searchable' => 0,
        'filterable' => 0,
        'sort_order' => 30,
        'comparable' => 0,
        'visible_on_front' => 0,
        'visible_in_advanced_search' => 0,
        'is_html_allowed_on_front' => 0,
        'is_configurable' => 1,
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, )); 
$installer->endSetup();

我的目标是创建这个属性,以便我可以在算术表达式中使用它,如小于或等于。

谢谢。

1个回答

1
你的收藏查询看起来像这样。
$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addFieldTofilter($attr_name, array(
        'eq' => Mage::getResourceModel('catalog/product')
            ->getAttribute($attr_name)
            ->getSource()
            ->getOptionId($attr_val)
    ))
    ->addAttributeToSelect('*')
    ->addFieldTofilter($metric, array('gteq' => $metric_val_min))
    ->addFieldTofilter($metric, array('lteq' => $metric_val_max))
   ->load();

你所描述的问题存在于小于号和大于号过滤部分。
这里我将向您展示一个实验,演示如何在SQL查询中将$metric视为整数,从而使查询在您的情况下起作用。
//create sql expression
$expr1 = 'CAST (' . $metric . ' AS UNSIGNED) >= ?';
$expr2 = 'CAST (' . $metric . ' AS UNSIGNED) <= ?';
$greaterEqCondtion = new Zend_Db_Expr($expr1);
$lesserEqCondition = new Zend_Db_Expr($expr2);

//applying filters which are relatively simple to express.
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldTofilter($attr_name, array(
        'eq' => Mage::getResourceModel('catalog/product')
            ->getAttribute($attr_name)
            ->getSource()
            ->getOptionId($attr_val)
));

//now going to apply filters which is relatively complex in this case.
$collection->getSelect()
    ->where($greaterEqCondtion, $metric_val_min)
    ->where($lesserEqCondition, $metric_val_max);

//we are set, let us load collection
$collection->load();

在这里$collection->getSelect()->where()表示一个表达式。该表达式会使字符串值在sql查询中被视为整数。我没有测试过这段代码,但你可以尝试一下。

如果你真的想要继续使用新属性,那么你需要创建一个新的安装资源或使用默认的eav安装资源(Mage_Eav_Model_Entity_Setup)。请参考这个教程了解如何设置基于EAV的模型。


感谢@Rajeev K Tomy的回答。我有一个后续问题。抱歉,我不太了解php,我不理解这段代码$greaterEqCondtion = new Zend_Db_Expr($expr1)。这是什么意思?我的理解是,这段代码将来自数据库的$metric数据进行转换。 - rodge
1
Zend_Db_Expr 是一个特殊的类,用于特定地准备 SQL 表达式。请记住,它将为您提供一个有效的字符串作为输出结果。因此 $expr 变量保存一个字符串类型的 SQL 表达式。该表达式被定义为将 $metric 变量视为整数。$metric 不是从数据库获取的,而是一个 PHP 变量,应在加载集合之前进行定义,尤其是 $metric 代表数据字段。 - Rajeev K Tomy
感谢@Rajeev K Tomy。这是我尝试代码时出现的错误。a:5:{i:0;s:491:"SELECT e.*, at_product_classification.value AS product_classification FROM catalog_product_entity AS e INNER JOIN catalog_product_entity_int AS at_product_classification ON (at_product_classification.entity_id = e.entity_id) AND (at_product_classification.attribute_id = '136') AND (at_product_classification.store_id = 0) WHERE (at_product_classification.value = '7') AND (CAST (max_ft_lbs AS UNSIGNED) >= 1500) AND (CAST (max_ft_lbs AS UNSIGNED) <= 2000) - rodge
继续:app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(871):Mage_Eav_Model_Entity_Collection_Abstract->_loadEntities(false, false) - rodge
这是由表达式 (CAST (max_ft_lbs AS UNSIGNED) >= 1500) AND (CAST (max_ft_lbs AS UNSIGNED) <= 2000) 添加的条件。在我看来,它看起来很好。阴谋围绕着 max_ft_lbs 字段展开,你需要找出原因。 - Rajeev K Tomy
感谢@Rajeev K Tomy的帮助。我会尝试两种解决方案。 - rodge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接