I'm trying to bind params to order by with expressions clause in Magento 2, my insecure code looks like:

$order = new \Zend_Db_Expr("POW((main_table.long - $this->_coordinates['lat']), 2) + POW((main_table.lat - {$this->_coordinates['lat']}), 2)");

                    'main_table.title ASC'

How could I bind both variables to the query?

1 Answer 1


So, I finnaly found the answer, I moved my filter codes to Collection class, when extending "Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection", you can use $this->addBindParam($name, $value). Follow my final model code:

$this->addBindParam(':lat', $args['coordinates']['lat']);
        $this->addBindParam(':long', $args['coordinates']['long']);

        $order = new \Zend_Db_Expr("POW((main_table.long - :long), 2) + POW((main_table.lat - :lat), 2) ASC");

            'main_table.title ASC'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.