Haversine formula in CakePHP 2.0 paginate fields not working - cakephp

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?

Daniel this post may have the answer although it is based on CakePHP 1.3. It involves binding the Models based on the aggregation.
http://cakephp.1045679.n5.nabble.com/Pagination-with-HasMany-and-SQL-Aggregation-td1307450.html

It looks like you may have an issue with your model name. If your application follows the correct CakePHP conventions, then you should be using Ride.field instead of Rides.Field. In CakePHP queries you want to reference the Model and not the table name.

Related

from date and to date not between in cakephp

I have two dates 30-01-2017 & 02-02-2017
Need a condition in cakephp to search record these two dates doesn't lies in between.
Please use this.I think this would be helpful.
$searchFirstdate = '30-01-2017';
$searchLastdate = '02-02-2017';
$this->ModelName->find(
'all',array(
'conditions'=>array(
'ModelName .date_field >= ' => $searchFirstdate,
'ModelName .date_field <= ' => $searchLastdate
)
)
);
There is a NOT BETWEEN operand for MYSQL, so you could easily use:
$this->ModelName->find('all', array(
'conditions' => array(
'ModelName.date_field NOT BETWEEN ? AND ?' => array('30-01-2017', '02-02-2017')
)
));
From CakePHP Manual

Cakephp find syntax for 'OR' and 'AND' complex join conditions

I have a task planning app and I'm attempting to fetch tasks based on two criteria: either they have a due date within the next 2 weeks, or the task itself is ending within 2 weeks. Here is my non-working code:
$conditions['AND'] = array(
'OR'=>array(
'AND'=>array(
'Task.due_date >'=> $now,
'Task.due_date <'=> $twfn
),
'AND'=>array(
'Task.end_time > '=> $now,
'Task.end_time <' => $twfn
)
)
);
There are additional conditions, hence the outer $conditions['AND']
The SQL log shows:
WHERE ((((`Task`.`end_time` > '2015-06-30') AND (`Task`.`end_time` < '2015-07-14')))
Which implies that the OR isn't being evaluated. If it's relevant, every task has an end_time, but not necessarily a due_date.
Any help or pointers in the right direction would be much appreciated!
use this: the ANDs should be in array, because you are giving the same key(AND) for the array and the last one will be used
$conditions['AND'] = array(
'OR'=>array(
array(
'AND'=> array(
array(
'Task.due_date >'=> $now,
),
array(
'Task.due_date <'=> $twfn
)
),
),
array(
'AND'=>array(
array(
'Task.end_time > '=> $now,
),
array(
'Task.end_time <' => $twfn
)
)
)
)
);
more examples from docs
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

Cake php multiply Find Conditions OR /AND

hey guys i have been struggling with this .i am a trying to select from a table using cakephp find all e.g
i want to select all from a table where (A == 1 AND B == 2) OR (A == 2 and B == 1) with just on query
here is my code so far
$conditions = array("OR"=>
array("Message.to_to"=>$daddy["User"]["id"],
"Message.from_from"=>$this->Auth->user("id")),
array("Message.to_to"=>$this->Auth->user("id"),
"Message.from_from"=>$daddy["User"]["id"])
);
To get your expected result (A == 1 AND B == 2) OR (A == 2 and B == 1), try nesting the 'and' conditions which are currently missing from your code.
You also need to specify the conditions parameter.
Try the following:
$conditions = array(
'conditions' => array(
"or"=> array(
"and" => array(
"Message.to_to"=>$daddy["User"]["id"],
"Message.from_from"=>$this->Auth->user("id"),
),
"and" => array(
"Message.to_to"=>$this->Auth->user("id"),
"Message.from_from"=>$daddy["User"]["id"],
),
),
),
);
Reference: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

cakephp: find ignores DISTINCT

i got the following cakephp find situation:
$data = $this->find('all', array(
'conditions' => array(
'Roster.league_id' => $league_id,
'Roster.season' => $season,
),
'fields' => array(
'DISTINCT Roster.player_id',
'Roster.league_id',
'Roster.fflteam_id',
'Roster.season',
'Roster.modified',
'Fflteam.name',
'Player.firstName',
'Player.lastName',
'Player.position'
),
'order' => array(
'Roster.player_id',
'Roster.modified DESC'),
'contain' => array(
'Fflteam',
'Player' => array(
'Stat' => array(
'conditions' => array(
'Stat.season' => $season),
'Scores'),
'Teamplayer')
)
));
There are more Roster-records with specific player_ids, thats why i try to use DISTINCT. I only need the most recent. Thats why i order the results by player_id and Roster.modified. But die DISTINCT command gets ignored.
e.g:
records:
id=1 player_id=1 modified=2012
id=2 player_id=1 modified=2013
id=3 player_id=1 modified=2014
id=4 player_id=2 modified=2014
id=5 player_id=2 modified=2013
result should be:
id=3 player_id=1 modified=2014
id=4 player_id=2 modified=2014
I don't see any syntax errors. Maybe there some commands are not possible together or my way of filtering is wrong. would be great if someone can help me.
DISTINCT does not give you one distinct field if there are multiple fields , if you have Distinct title it will provides distinct title .
If you have distinct id , title it will output distinct combination of rows with same id and title.
This is how DISTINCT works.
Try GroupBy
Check this page to understand what to use when http://www.mysqltutorial.org/mysql-distinct.aspx
As AgRizzo suggested i am using a query now.
Its split into 2 Parts. The first gets all entries with the one2one relations:
$this->query("SELECT *
FROM (SELECT * FROM `rosters` AS `Roster1` WHERE " . $conditions . " ORDER BY `Roster1`.`modified` DESC) AS `Roster`
LEFT JOIN `fflteams` AS `Fflteam` ON (`Roster`.`fflteam_id` = `Fflteam`.`id`)
LEFT JOIN `players` AS `Player` ON (`Roster`.`player_id` = `Player`.`id`)
GROUP BY `Player`.`id` ORDER BY `Roster`.`player_id` ASC");
The second part gets all one2many relations with its relation:
$this->Stat->find('all', array(
'conditions' => $conditions,
'contain' => array(
'Scores')
));
at the end i merge those 2 arrays

CakePHP Search between 2 Date Records

I am building a small Web App that lets users reserve Office Rooms and Equipment. For the Reservation they enter a Start and an End Date.
When a user tries to find out if any (for example) car is available on 2012-10-23, and the database holds reservation date records of Start: 2012-10-20 and End: 2012-10-25 for (lets say) all the cars, how do I include all the dates between my date entries in the search?
The $date variable gets it's value from the Date Search Form Field.
This, unfortunately does not work, and I can't figure out how to use daysAsSql for this query:
$conditions = array(
'conditions' => array(
'? BETWEEN ? AND ?' => array($date,'Equipment.date_start','Equipment.date_end'),
)));
$this->set('equipments', $this->Equipment->find('all', $conditions));
There is a simpler solution to this, but thanks for the help:
(As a condition in the find:)
array('Equipment.date_start <= ' => $date,
'Equipment.date_end >= ' => $date
),
In case if you have one date and you want to use BETWEEN
$date_start = start date;
$date_end = date_end;
$conditions = array(
'conditions' => array(
'date(Equipment.create) BETWEEN ? AND ?' => array($date_start, $date_end),
)));
$this->set('equipments', $this->Equipment->find('all', $conditions));
This is the case if you have From AND To
in your case
$date_start = start date;
$date_end = date_end;
$conditions = array(
'conditions' => array(
'Equipment.start_date >=' => array($date_start),
'Equipment.end_date <=' => array($date_end)
));
$this->set('equipments', $this->Equipment->find('all', $conditions));
Here is CakePHP BETWEEN query example.
I'm defining my arrays as variables, and then using those variables in my CakePHP find function call:
// just return these two fields
$fields = array('uri', 'page_views');
// use this "between" range
$conditions = array('Event.date BETWEEN ? and ?' => array($start_date, $end_date));
// run the "select between" query
$results = $this->Event->find('all',
array('fields'=>$fields,
'conditions'=>$conditions));
Ref from
$start = date('Y-m-d');
$end = date('Y-m-d', strtotime('+1 month'));
$conditions = array('Event.start <=' => $end, 'Event.end >=' => $start);
$this->Event->find('all', array('conditions' => $conditions));
If you are on cakephp 2.0 this is the answer http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::daysAsSql
If you are on cakephp 1.3 you can use TimeHelper, just import it and use the same function as per example in documentation here http://book.cakephp.org/1.3/en/view/1471/Formatting
You cannot use database columns with this BETWEEN-syntax. If you assign strings in the array, CakePHP will quote them. Same for numeric values depending on your database setup.
CakePHP will quote the numeric values depending on the field type in your DB.
– see http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions
If you still want to use the BETWEEN, you can write your queries into the array key because CakePHP will not escape the keys, but only the values.
CakePHP only escapes the array values. You should never put user data into the keys. Doing so will make you vulnerable to SQL injections.
– see http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions
Concerning your problem:
$this->Model->find('all', array(
'conditions' => array(
'"YYYY-MM-DD" BETWEEN Model.date_start AND Model.date_end',
),
));
You can even work with MySQL date and time functions:
$this->Model->find('all', array(
'conditions' => array(
'CURDATE() BETWEEN Model.date_start AND Model.date_end',
),
));
If you use date/time functions, do not quote them. If you use a specific date put it in quotes.

Resources