Running Raw SQL Query
Posted on June 20, 2017, 1:29 pm, by admin, under
Magento 1.x.
Place this function to Resource Model
public FUNCTION getMarkers($lat, $lng) {
/**
* Search radius in km
*/
$searchRadius = 10;
/**
* Get the resource model
*/
$resource = Mage::getSingleton('core/resource');
/**
* Retrieve the read connection
*/
$readConnection = $resource->getConnection('core_read');
/**
* Retrieve our table name
*/
$table = $resource->getTableName('storelocator/entity');
/**
* Build the query
*/
$distanceCalc = 'SQRT(POW(lat - ' . $lat . ', 2) + POW(lng - ' . $lng . ' , 2)) * 100';
$query = 'SELECT ROUND('.$distanceCalc. ',2) AS distance_km, '
. ' name, lat, lng FROM ' . $table
. ' WHERE distance_km < ' . $searchRadius;
/**
* Execute the query and return the results
*/
RETURN $readConnection->fetchAll($query);
} |
public function getMarkers($lat, $lng) {
/**
* Search radius in km
*/
$searchRadius = 10;
/**
* Get the resource model
*/
$resource = Mage::getSingleton('core/resource');
/**
* Retrieve the read connection
*/
$readConnection = $resource->getConnection('core_read');
/**
* Retrieve our table name
*/
$table = $resource->getTableName('storelocator/entity');
/**
* Build the query
*/
$distanceCalc = 'SQRT(POW(lat - ' . $lat . ', 2) + POW(lng - ' . $lng . ' , 2)) * 100';
$query = 'SELECT ROUND('.$distanceCalc. ',2) AS distance_km, '
. ' name, lat, lng FROM ' . $table
. ' WHERE distance_km < ' . $searchRadius;
/**
* Execute the query and return the results
*/
return $readConnection->fetchAll($query);
}