This is my first post on this site, so please be kind.
I have no trouble making this work using the find() method, but when I try to run it using the paginate component, no luck. I am simply trying to run a Haversine formula in the fields options to calculate distance. In the Cake Book, it says "You can also include other find() options, such as fields:". There is no caveat mentioned.
So, this is what I am trying to run:
$this->paginate = array(
'joins' => array(
array(
'table' => 'ride_types',
'alias' => 'RideTypes',
'type' => 'inner',
'conditions'=>array('RideTypes.id = Rides.ride_type')
)
),
'group' => array(
"Rides.id HAVING distance < $filter_radius AND ride_time >= '$filter_earliest_time' AND ride_time <= '$filter_latest_time' AND ride_date >= '$filter_earliest_date' AND ride_date <= '$filter_latest_date'"
),
'fields' => array(
"( 3959 * acos( cos( radians($filter_lat) ) * cos( radians( Rides.lat ) ) * cos( radians( Rides.lng ) - radians($filter_lng) ) + sin( radians($filter_lat) ) * sin( radians( Rides.lat ) ) ) ) AS distance"
),
'limit' => 5,
'page' => $paginate_page,
'order' => array('Rides.start_time DESC'),
);
$rides = $this->paginate('Rides',$options['conditions']);
I get this error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause'
SQL Query: SELECT COUNT(*) AS count
FROM velobuddies
.rides
AS Rides
inner JOIN velobuddies
.ride_types
AS RideTypes
ON (RideTypes
.id
= Rides
.ride_type
) WHERE 1 = 1 GROUP BY Rides
.id
HAVING distance < 5
Again, if I apply all these exact same options to the find('all') method, it works perfectly, so I know the logic is correct... or at least workable.
So what is going on here?