【问题标题】:Add custom sql in getProductCollection, or insert geolocation formula在 getProductCollection 中添加自定义 sql,或插入地理位置公式
【发布时间】:2012-12-02 17:02:59
【问题描述】:

我尝试在以下文章的帮助下按地理位置数据对 magento 中的产品列表进行排序:https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=hu-HU

我在我的模块中覆盖了 Mage_Catalog_Block_Product_List 以便我可以访问这行代码:

$this->_productCollection = $layer->getProductCollection();

拥有 _productCollection 对象后,我想将公式添加为“虚拟”属性“距离”,以便可以使用它对查询进行排序。这是我的想法:

$this->_productCollection = $layer->getProductCollection()
->addAttributeToSelect('distance','I insert my custom formula here')
->addAttributeToSort('distance','DESC');

我知道 API 不是为处理这种语法而设计的,但我认为它可以帮助人们理解这个想法。

这是我要添加的公式:

SELECT ( 3959 * acos( cos( 弧度(37) ) * cos( 弧度( lat ) ) * cos(弧度(lng)-弧度(-122))+ sin(弧度(37))* sin( 弧度( lat ) ) ) ) AS 距离

知道怎么做吗?

【问题讨论】:

    标签: magento geolocation


    【解决方案1】:

    如果latlng是你产品的属性,你可以使用addExpressionAttributeToSelect():

    $this->_productCollection
    ->addExpressionAttributeToSelect('distance', '(select ... {{lat}} ... {{lng}} ... )', array('lat'=>'lat', 'lng'=>'lng'));
    

    在自定义选择查询中,latlng需要用双括号括起来(如图)。确保您的选择语句在括号内。

    【讨论】:

    • 非常感谢,它运行良好,“->addExpressionAttributeToSelect”对我未来的 Magento 开发非常有帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 2017-09-03
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    相关资源
    最近更新 更多