I am doing a find on a model as shown below however the SQL being generated is wrong due to which I am getting incorrect results.
$bookings = $this->Booking->find('all', array(
'conditions' => array(
'Booking.created BETWEEN ? AND ?' => array(
date('Y-m-d H:i:s', strtotime('-24 hours')),
date('Y-m-d H:i:s', strtotime('-12 hours'))
),
'OR' => array(
'Booking.payment_status' => 3,
'Booking.payment_status' => 2
)
)
));
WHERE condition being generated by this find is (as shown by cake debugkit)
WHERE `Booking`.`created` BETWEEN '2013-03-27 11:01:57' AND '2013-03-27 23:01:57' AND `Booking`.`payment_status` = 2
And the where condition I am trying to create is
WHERE `Booking`.`created` BETWEEN '2013-03-27 11:01:57' AND '2013-03-27 23:01:57' AND (`Booking`.`payment_status` = 2 OR `Booking`.`payment_status` = 3)
Any ideas what I am doing wrong here. Quick help would be highly appreciated. Thanks!
A frequently made PHP beginner mistake - using a key in an array more than once:
'OR' => array(
'Booking.payment_status' => 3,
'Booking.payment_status' => 2
)
It should read
'OR' => array(
array('Booking.payment_status' => 3),
array('Booking.payment_status' => 2)
)
Related
I want to write this query in CakePHP:
SELECT * FROM pm_management.chk_master_xs_tasks
where (eq_model = 'D11 R' OR eq_model = 'TODOS') AND (pm_type = 'XD' OR pm_type = 'XS');
I have tried severals ways like this:
$this->ChkMasterXsTask->find('all', array(
'conditions' => array('AND' => array(
array('OR' => array('ChkMasterXsTask.eq_model' => $eq_model,
'ChkMasterXsTask.eq_model' => 'TODOS')),
array('OR' => array('ChkMasterXsTask.pm_type' => $pm_type,
'ChkMasterXsTask.pm_type' => 'XS'))
)
),
));
But the result is not the expected. Also I saw related questions but doesn't work for me.
Thank you so much for your answers.
Your problem arises from this code (and this problem is repeated for eq_model)
array('ChkMasterXsTask.pm_type' => $pm_type,
'ChkMasterXsTask.pm_type' => 'XS')
which tries to assign 2 values to the same key of that associative array. You could rewrite your code to this:
array(array('ChkMasterXsTask.pm_type' => $pm_type),
array('ChkMasterXsTask.pm_type' => 'XS'))
Another option which improves readability, uses SQL's IN function. The equivalent SQL becomes
SELECT *
FROM pm_management.chk_master_xs_tasks
WHERE eq_model IN ('D11 R', 'TODOS')
AND pm_type IN ('XD', 'XS');
Which should make your find a little easier write:
$this->ChkMasterXsTask->find('all', array(
'conditions' => array(
'ChkMasterXsTask.eq_model' => array($eq_model, 'TODOS'),
'ChkMasterXsTask.pm_type' => array($pm_type, 'XS'))
));
just I see a error y your code, there is a comma, that shouldn't be there.
$this->ChkMasterXsTask->find(
'all', array(
'conditions' => array(
'AND' =>
array('OR' => array(
array('ChkMasterXsTask.eq_model' => $eq_model),
array('ChkMasterXsTask.eq_model' => 'TODOS')
),
array('OR' =>
array('ChkMasterXsTask.pm_type' => $pm_type),
array('ChkMasterXsTask.pm_type' => 'XS')
)
))
));
i have a table in which i want to get only two columns data .. right now i am using findAll method ... i don't know how can I get the specific two fields data in CakePHP
$recentContacts = $this->Contact->find('all',
array(
'order'=>'Contact.idContacts DESC',
'limit' => 6,
'conditions' => array(
'Contact.User_id' => $id)));
in my contacts table there are two fields one is "name" and other is "number"
which I want to extract ...
You can do this by adding fields attribute.
$recentContacts = $this->Contact->find('all',
array
(
'order'=> array( 'Contact.id' , 'Contacts DESC'),
'limit' => 6,
'fields' => array(
'Contact.name',
'Contact.number'
),
'conditions' => array
(
'Contact.User_id' => $id
)
));
you can use like this way with your same code to add fields
$recentContacts = $this->Contact->find('all',
array(
'order'=>'Contact.idContacts DESC',
'limit' => 6,
'fields' => array(
'Contact.name',
'Contact.number'
),
'conditions' => array(
'Contact.User_id' => $id)));
in previous answer they have changed you id instead of idContacts, you can just copy my code and solve your problem.
let me know if i can help you more.
actually, i want to meet this sql requirement:
... where (type_id = 6 OR type_id = 8) and status = 2 ....
but i don NOT know how to set conditions in $paginate.
var $paginate = array(
'Student'=>array(
'limit' => 10,
'fields'=>array('Student.id','Student.firstname','Student.lastname'),
'order' =>array('Student.id'=>'desc'),
'conditions' => ?,
),
);
any one knows how to set conditions ?
Based on this site http://book.cakephp.org/1.3/view/1030/Complex-Find-Conditions
(It's not the lastes release, but try it)
var $paginate = array(
'Student'=>array(
'limit' => 10,
'fields'=>array('Student.id','Student.firstname','Student.lastname'),
'order' =>array('Student.id'=>'desc'),
'conditions' => array(
'Student.type_id' => array(6, 8),
'Student.status' => 2
)
),
);
or
var $paginate = array(
'Student'=>array(
'limit' => 10,
'fields'=>array('Student.id','Student.firstname','Student.lastname'),
'order' =>array('Student.id'=>'desc'),
'conditions' => array(
'Student.status' => 2,
"OR" => array (
'Student.type_id' => 6,
'Student.type_id' => 8
)
)
),
);
EDIT: This second version cannot be used in this situation, because now the OR array have two elements with the same keys and will check only the last (8)
[N.B.] Excuse me if I made some syntax error :)
I am having trouble getting any find operations to work using the SQL function NOW() in my conditions.
I am effectively trying to build a find query that says:
Desired SQL:
WHERE (NOW() BETWEEN Promotion.start AND Promotion.end) AND Promotion.active = 1
I have tried many combinations but no matter what I do when using NOW() in the condition, it doesn't work because the query Cake builds puts ' quotation marks around the model fields so they are interpreted by MySQL as a string.
$this->find('all', array(
'conditions' => array(
'(NOW() BETWEEN ? AND ?)' => array('Promotion.start', 'Promotion.end'),
'Promotion.active' => 1
)
));
CakePHP created SQL:
Notice the single quotes around the model fields in the BETWEEN(), so they are treated as strings.
WHERE (NOW() BETWEEN 'Promotion.start' AND 'Promotion.end') AND `Promotion`.`active` = '1'
This doesn't work either.
$this->find('all', array(
'conditions' => array(
'NOW() >=' => 'Promotion.start',
'NOW() <=' => 'Promotion.end',
'Promotion.active' => 1
)
));
I know why these solutions don't work. It's because the model fields are only treated as such if they are the array key in the conditions, not the array value.
I know I can get this to work if I just put the whole BETWEEN() condition as a string:
$this->find('all', array(
'conditions' => array(
'NOW() BETWEEN Promotion.start AND Promotion.end',
'Promotion.active' => 1
)
));
Another example of the same problem is, which is simpler to understand:
Desired SQL:
WHERE Promotion.start > NOW() AND Promotion.active = 1
So I try this:
$this->find('all', array(
'conditions' => array(
'Promotion.start >' => 'NOW()',
'Promotion.active' => 1
)
));
And again it doesn't work because Cake puts ' quotations around the NOW() part.
CakePHP created SQL:
WHERE `Promotion`.`start` > 'NOW()' AND `Promotion`.`active` = '1''
$this->find('all', array(
'conditions' => array(
'NOW() BETWEEN Promotion.start AND Promotion.end',
'Promotion.active' => 1
)
));
Better to not use NOW() as its a function and functions don't use indexes. A better solution would be:
$this->find('all', array(
'conditions' => array(
"'" . date('Y-m-d') . "' BETWEEN Promotion.start AND Promotion.end",
'Promotion.active' => 1
)
));
I have a start and an end date in my database and a $date variable from a form field. I am now trying to query all the rows where $date is either = start/end date in the db, or ANY date between those two.
It's kind of the opposite of what is described in the docs of how daysAsSql works. I can't figure out how to get it to work. The following line does not work as a find condition in the controller:
'? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),
Any help is greatly appreciated. This is driving me crazy.
Here is the complete Query and corresponding SQL:
$conditions = array(
'conditions' => array(
'and' => array(
'? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),
'Item.title LIKE' => "%$title%",
'Item.status_id =' => '1'
)));
$this->set('items', $this->Item->find('all', $conditions));
WHERE (('2012-10-06' BETWEEN 'Item.date_start' AND 'Item.date_end') AND (`Item`.`title` LIKE '%%') AND (`Item`.`status_id` = 1))
$conditions = array(
'conditions' => array(
'and' => array(
array('Item.date_start <= ' => $date,
'Item.date_end >= ' => $date
),
'Item.title LIKE' => "%$title%",
'Item.status_id =' => '1'
)));
Try the above code and ask if it not worked for you.
Edit:
As per #Aryan request, if we have to find users registered between 1 month:
$start_date = '2013-05-26'; //should be in YYYY-MM-DD format
$this->User->find('all', array('conditions' => array('User.reg_date BETWEEN '.$start_date.' AND DATE_ADD('.$start_date.', INTERVAL 30 DAY)')));
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
General Example For CakePHP 2.x Query
$_condition = array("TABLENAME.id" => $id, "TABLENAME.user_id" => array_unique($_array), 'date(TABLENAME.created_at) BETWEEN ? AND ?' => array($start_date, $end_date));
$result_array = $this->TABLENAME->find("all", array(
'fields' => array("TABLENAME.id", "TABLENAME.user_id", 'TABLENAME.created_at'),
"conditions" => $_condition,
"group" => array("TABLENAME.id"), //fields to GROUP BY
'joins' => array(
array(
'alias' => 'T2',
'table' => 'TABLENAME2',
'type' => 'LEFT',
'conditions' => array('TABLENAME.t_id = TABLENAME2.t_id')
),
array(
'alias' => 'T3',
'table' => 'TABLENAME3',
'type' => 'LEFT',
'conditions' => array(
'IF(
TABLENAME.t3_id > 0,
T2.f_id = T3.f_id,
TABLENAME.ff_id = T2.ff_id
)'
)
),
),
'recursive' => 0
)
);
This is more efficient and understandable IN BETWEEN query in cakephp 2.x
$testing_log_device_site_name = $testingLogData['TestingLogDevice']['Siteid'];
$conditions = array('TestingLogDevice.dateee BETWEEN ? and ?' => array($start_date, $end_date));
$results = $this->TestingLogDevice->find('all',
array(
'fields'=>array('dateee','timeee','Siteid'),
'conditions'=>array($conditions, 'TestingLogDevice.Siteid'=>$testing_log_device_site_name)
)
);
pr($results);
$data=$this->post->find('all')->where([ 'id'=>$id,
'price between'=>$price1,'and'=>$price2])->toArray();
This query works as following:
select * from post where id=$id and price between $price1 and $price2;
" -- 'price between'=>$price1 --" become "price between $price1"
Use this
$today = new DateTime( date('Y-m-d'));
$fiveYearsBack = $today->sub(new DateInterval('P5Y'));