CakePHP Search between 2 Date Records - cakephp

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.

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 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 :Find all query conditions questions

I'm trying to format a find condition to generate a set of data to be later printed to an excel view via PhPExcel. The intended functionality is that if the start_date and the end_date fields match for the entry, the entry is included in the array. If the fields do not match, it's excluded.
$my_data = $this-> RecordsMaster-> find("all", array(
'conditions' => array(
"RecordsMaster.start_date" == "RecordsMaster end_date"
)
));
The problem lies in the fact that the statement above doesn't actually do anything. Rows from mySQL table with start and end dates that differ are included in the data dump to excel.
The conditions in the question don't mean much
The conditions in the question are equivalent to:
$my_data = $this-> RecordsMaster-> find("all", array(
'conditions' => array(
false
)
));
Which will return no records, since it will generate the following sql:
WHERE 0;
Don't use key => value to match fields
Correcting the typos in the code in the question would still not generate desired sql - it would generate:
WHERE `RecordsMaster`.`start_date` = "RecordsMaster end_date"
I.e. where start_date is that litteral string.
The intended functionality is that if the start_date and the end_date fields match for the entry
To generate WHERE field = otherfield you can specify as a string:
$my_data = $this-> RecordsMaster-> find("all", array(
'conditions' => array(
"RecordsMaster.start_date = RecordsMaster.end_date"
)
));

CakePHP find joined records with conditions on each table

I want to see all the records from a join, setting a WHERE condition on each side of the join.
For example, I have LOAN and BORROWER (joined on borrower.id = loan.borrower_id). I want the records where LOAN.field = 123 and BORROWER.field = 'abc'.
The answers here (this one, for example) seem to say that I should use Containable.
I tried that. Here's my code:
$stuff = $this->Borrower->find('all', array(
'conditions' => array(
'Borrower.email LIKE' => $this->request->data['email'] // 'abc'
),
'contain'=>array(
'Loan' => array(
'conditions' => array('Loan.id' => $this->request->data['loanNumber']) // 123
)
)
));
I expected to have a single result because in my data, there is only one joined record with both of those conditions. Instead, I get two results,
Result 1 is {Borrower: {field:abc, LOAN: {field: 123} } // correct
Result 2 is {Borrower: {field:abc, LOAN: {NULL} } // incorrect
When I look at the SQL that CakePHP used, I don't see a join. What I see is two separate queries:
Query 1: SELECT * from BORROWER // (yielding 2 IDs),
Query 2: SELECT * FROM LOAN WHERE borrower_id in (IDs)
This is not what I want. I want to join the tables, then apply my conditions. I could easily write the SQL query, but am trying to do it the Cake way since we've adopted that framework.
Is it possible?
Try to do something like this:
$options['conditions'] = array(
'Borrower.email LIKE' => $this->request->data['email'] // 'abc',
'loan.field' => '123' )
$options['joins'] = array(
array('table' => 'loans',
'alias' => 'loan',
'type' => 'INNER',
'conditions' => array(
'borrower.id = loan.borrower_id')
)
);
$options['fields'] = array('borrower.email', 'loan.field');
$test = $this->Borrower->find('all', $options);
You should see a SQL statement like:
SELECT borrower.email, loan.field
FROM borrowers AS borrower
INNER JOIN loans AS loan
ON borrower.id = loan.borrower_id
AND loan.field = '123'
WHERE borrower.email = 'abc'
Your results will be in an array
{Borrower: {field:abc} LOAN: {field: 123} }
You will find more information in this document.
I think I'll accept Jose's answer because it's exactly what I want. But I did notice that I didn't need any fancy tricks -- no joins or contains -- if I used the other model as my starting point.
A Borrower hasMany Loans, and a Loan belongsTo a Borrower. Using Loan as my model, Cake will automatically join the tables, but not using Borrower.
$this->Loan->find('all', array( // Not $this->Borrower->find() !
'conditions' => array(
'Borrower.field' => 'abc',
'Loan.field' => 123
)
));

convert query() to find() cakephp

$v = array(1,11.38,15.8);
$sortByPrice = $this->Product->query
(
"SELECT *,
CASE `currency`
WHEN '1' THEN $v[0]
WHEN '2' THEN $v[1]
WHEN '3' THEN $v[2]
END AS 'ratio'
FROM products
ORDER BY price*ratio DESC
"
);
i want to convert what is above to a find function
i tried something like that(but it do not work)..
$v = array(1,11.38,15.8);
$bla = $this->Product->find('all', array(
'conditions' => array(
'Product.currency',
'((
CASE WHEN
Product.currency=1 THEN $v[0]
Product.currency=2 THEN $v[1]
Product.currency=3 THEN $v[2]
END
)) AS ratio'),
'order' => 'ratio',
'limit' => 10
));
can somebody to convert query into find
You are putting into the conditions key. Move it into the fields key or make it a virtualField - that may work.
Edit: also $v[0] in single quotes will not actually replace it with the variable - it will just appear as that text.

Resources