0

I want to write this query into Magento 2 formate

SELECT vendor_id, vendor_name, (
    6371 * ACOS( COS( RADIANS( 45.4102961 ) ) * COS( RADIANS( vendor_latitude ) ) * COS( RADIANS( vendor_longitude ) - RADIANS( - 93.2434978 ) ) + SIN( RADIANS( 45.4102961 ) ) * SIN( RADIANS( vendor_latitude ) ) )
    ) AS distance
    FROM udropship_vendor
    HAVING distance <30
    ORDER BY distance
    LIMIT 0 , 20

Here is my query

$collection = $this->vendor->create();
    $collection->addFieldToSelect(array('vendor_id', 'vendor_name', 'vendor_latitude', 'vendor_longitude', 'zip', 'street', 'city'));
    $collection>addExpressionFieldToSelect('distance', new Zend_Db_Expr('( 6371 * acos( cos( radians('.$latitudeFrom.') ) * cos( radians( {{vendor_latitude}}) ) * cos( radians( {{vendor_longitude}}) - radians('.$longitudeFrom.') ) + sin( radians('.$latitudeFrom.') ) * sin( radians( {{vendor_latitude}}) ) ) )'), array('vendor_latitude'=>'vendor_latitude', 'vendor_longitude'=>'vendor_longitude'));

    $collection->getSelect()->having('distance > 10');

    return $collection;
2
0

Hi thank you for help the answers is here

Please make sure your collection is extending

\Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

You can use with pagination like this method

public function getNearestVendorList()
{
    $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
    $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 5;
    $redious = 50;

    $cookieValue = $this->helper->getCookie();
    if($cookieValue){
        $latitudeFrom = $cookieValue['latitude'];
        $longitudeFrom = $cookieValue['longitude'];
    }else{
        $latitudeFrom = $this->helper->getDefaultLatitude();
        $longitudeFrom = $this->helper->getDefaultLongitude();
    }
    $distance = $this->helper->getDistanceRadious();

    $collection = $this->vendor->create();
    $collection->addFieldToSelect(['vendor_id', 'vendor_name', 'vendor_latitude', 'vendor_longitude', 'zip', 'street', 'city']);
    $collection->setPageSize($pageSize);
    $collection->setCurPage($page);
    $collection->addExpressionFieldToSelect('distance', '( 6371 * acos( cos( radians('.$latitudeFrom.') ) * cos( radians( {{vendor_latitude}}) ) * cos( radians( {{vendor_longitude}}) - radians('.$longitudeFrom.') ) + sin( radians('.$latitudeFrom.') ) * sin( radians( {{vendor_latitude}}) ) ) )', array('vendor_latitude'=>'vendor_latitude', 'vendor_longitude'=>'vendor_longitude'));

    return $collection;
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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