DATE_SUB( NOW () INTERVAL 2 WEEK) Failed in CakePHP

Written by James McDonald

April 21, 2013

I had this:

public function recent_history($weeks = null) {
	$this -> MapHistory -> recursive = 0;

	$this->paginate = array (
		'conditions' => array(
			'OR' => array(
				'MapHistory.date_returned >' => 'DATE_SUB( CURRENT_DATE(), INTERVAL 2 WEEK)',
				'MapHistory.date_loaned >' => DATE_SUB( CURRENT_DATE(), INTERVAL 2 WEEK)'
			)
	         ),
				'order' => array(
					'MapHistory.date_returned' => 'ASC',
					'MapHistory.date_loaned' => 'ASC'
					),
				'limit' => 50
			);

		$this -> set('mapHistories', $this -> paginate('MapHistory'));
}

But it was failing miserably because it was passing ‘DATE_SUB( CURRENT_DATE(), INTERVAL 2 WEEK)’ with the quotes and MYSQL wasn’t executing the statements.

Following this => http://stackoverflow.com/questions/9076152/way-for-using-date-subcurdate-interval-2-day-condition-in-cakephp

I just removed the array key => value stuff and placed it all in one set of single quotes and it worked fine:

public function recent_history($weeks = null) {
	$this -> MapHistory -> recursive = 0;

	$this->paginate = array (
		'conditions' => array(
			'OR' => array(
				'MapHistory.date_returned > DATE_SUB( CURRENT_DATE(), INTERVAL 2 WEEK)',
				'MapHistory.date_loaned > DATE_SUB( CURRENT_DATE(), INTERVAL 2 WEEK)'
			)
	         ),
				'order' => array(
					'MapHistory.date_returned' => 'ASC',
					'MapHistory.date_loaned' => 'ASC'
					),
				'limit' => 50
			);

		$this -> set('mapHistories', $this -> paginate('MapHistory'));
}

 

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Squarespace Image Export

To gain continued access to your Squarespace website images after cancelling your subscription you have several...

MySQL 8.x GRANT ALL STATEMENT

-- CREATE CREATE USER 'tgnrestoreuser'@'localhost' IDENTIFIED BY 'AppleSauceLoveBird2024'; GRANT ALL PRIVILEGES ON...

Exetel Opt-Out of CGNAT

If your port forwards and inbound and/or outbound site-to-site VPN's have failed when switching to Exetel due to their...