I am getting a list of Rate information from the database with conditions. However I wish to add another condition of minimum Stay if the date range has a date in it and rateID form another table called minimum stay. The Rate table already has a min stay but on certain dates I want to over ride this if the date falls within the date range I pass.
I am new to cakephp so I am unsure how to check the minStay table for dates in the date range. Then get the largest minStay and the add it to the condition.
Default minStay in Rate table is 1
Here is the data in the minStay table:
date:31-10-2011 rateID:21 minStay:2
date:1-11-2011 rateID:21 minStay:3
Results: If date range is 31-10-2011 to 2-11-2011 / 2 nights then no results. If 3 nights or more then result.
I hope I have explaind that correctly.
Variable containing the date range is $todays
$conditions = array(
'Rate.enabled'=>1,'Rate.is_corporate'=>$is_corporate, 'Rate.minimum_stay <='=>$days,
'Rate.valid_from < '=>$date_start,'Rate.valid_to >'=>$date_end,
'OR'=>array('Rate.maximum_stay'=>0,'Rate.maximum_stay >='=>$days)
);
$order = 'Rate.list_no';
$this->Rate->contain('Room.id','Room.title','Room.max_adults','Room.max_children');
$rates = $this->Rate->find('all',array('conditions'=>$conditions,'order'=>$order));
I think i understood what you want to do, if not please comment.
To do that you need to do somthing like this (assuming you have something like has many MinStay)
$conditions = array(
'Rate.enabled'=>1,'Rate.is_corporate'=>$is_corporate, 'Rate.minimum_stay <='=>$days,
'Rate.valid_from < '=>$date_start,'Rate.valid_to >'=>$date_end,
'OR'=>array('Rate.maximum_stay'=>0,'Rate.maximum_stay >='=>$days),
'AND' => array('OR'=> array('minStay.date BETWEEN ? AND ?' => array($date_start,$date_end)),
'minStay.minimum_stay <' =>$days )
);
$order = 'Rate.list_no';
$fields = array ('Room.id','Room.title','Room.max_adults','Room.max_children');
$rates = $this->Rate->find('all',array('conditions'=>$conditions,'order'=>$order, 'fields'=> $fields));
I think that will do it, hope it works for you
Related
I am trying get month wise sum of amount from transactions table. I have written below cakephp function to get my desire output.
public function getLastSixMOnthsExpenses($since_date, $t_type)
{
$query = $this->find()->select([
'month' => $this->find()->func()->monthname([
'created' => 'identifier'
]),
'amount' => $this->find()->func()->sum('Transactions.amount')
])
->where([
'Transactions.created >='=> $since_date->modify('6 months ago')->startOfMonth(),
'Transactions.created <='=> $since_date->endOfMonth(),
'Transactions.transaction_type '=>$t_type
])
->group(['month'])
->order(['Transactions.created'=>'ASC'])
;
return $query;
}
I am getting below error
Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'kjoumaa_kamaljoumaa.Transactions.created' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Without change sql mode , How can I run group by here ?
Order on an aggregate instead.
Since you group by the month, all created fields in those groups will be of one and the same month, eg all created fields in one group will point to either an earlier or a later date than the fields of another group, so you could simply pick either the min or the max value out of a group:
->orderAsc(function (
\Cake\Database\Expression\QueryExpression $exp,
\Cake\ORM\Query $query
) {
return $query->func()->min(
$query->identifier('Transactions.created')
);
})
ORDER BY MIN(Transactions.created) ASC
Also if you would select the month as a number instead of as a name, you could order on that field.
Hello i am trying to write an existing xlsx file using phpspreadsheet with setActiveSheetIndexByName(sheetname) and setcellvalue with reference and value, but it updates only the last record. spent more than 12 hours on this.
i tried foreach instead of while and used a counter to increment, but none worked.
<?php
include_once('db.php');
$prospect = $_REQUEST['prospect'];
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$sql1 = mysqli_query($db,"select filename,sheetname, row, responsecol,compliancecol,response, compliance from spreadsheet where `prospect`='$prospect' and response <>'' order by row");
//$row=1;
while($row1 = mysqli_fetch_assoc($sql1))
{
$filename= $row1['filename']; //test.xlsx
$sheetname= $row1['sheetname']; // mysheet
$responsecol= $row1['responsecol'].$row1['row']; //D1
$response= $row1['response']; //response
$compliancecol= $row1['compliancecol'].$row1['row']; //C1
$compliance= $row1['compliance']; //compliance
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filename);
$spreadsheet->setActiveSheetIndexByName($sheetname)
->setCellValue($compliancecol,$compliance)
->setCellValue($responsecol,$response);
//$row++;
}
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save("newfile.xlsx");
exit;
?>
i wish each of the row from mysqli result updates each reference cell with value.
The easys way is to set a Limit of 1 to your MySQL query. That takes only one value from your data. If you will the last you should sort DESC.
$sql1 = mysqli_query($db,"select filename,sheetname, row, responsecol,compliancecol,response, compliance from spreadsheet where `prospect`='$prospect' and response <>'' order by row DESC LIMIT 1");
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 Group by problem in paginate..
This is my table structure
Friends Table
`id` int(11) NOT NULL AUTO_INCREMENT,
`user1_id` int(11) NOT NULL DEFAULT '0',--> UserFrom
`user2_id` int(11) NOT NULL DEFAULT '0',---> UserTo
I need to get all the unique records of one user and his user1_id = 100
There are lot of duplicate values in user2_id. I need to get the unique values
While i trying this code it returns only first 12 values(according to limit).
If i commented the group by line then all records are displaying (including duplicate values)
$this->paginate = array(
'conditions' => $conditions,
'contain'=>array(
'UserFrom'=>array(
'fields'=>array(
'UserFrom.id',
'UserFrom.first_name',
'UserFrom.last_name',
),
),
'UserTo'=>array(
'fields'=>array(
'UserTo.id',
'UserTo.first_name',
'UserTo.last_name',
)
)
),'limit' => 12,
'order' => array('Friend.id' => 'desc'),
'recursive'=>0,
'fields'=>array('DISTINCT Friend.user2_id','Friend.*'),
'group'=>array('UserTo.id'),
);
This is my sql query on that page
SELECT COUNT(*) AS count FROM friends AS Friend LEFT JOIN users AS UserTo ON (Friend.user2_id = UserTo.id) LEFT JOIN users AS UserFrom ON (Friend.user1_id = UserFrom.id) WHERE UserFrom.nick_name = 'shyam' AND Friend.status = 'friend' GROUP BY UserTo.id
SELECT DISTINCT Friend.user2_id, Friend.*, UserTo.id, UserTo.first_name, UserTo.last_name, UserTo.nick_name, UserTo.name, UserTo.icon_medium, UserTo.icon_big, UserFrom.id, UserFrom.first_name, UserFrom.last_name, UserFrom.nick_name, UserFrom.name FROM friends AS Friend LEFT JOIN users AS UserTo ON (Friend.user2_id = UserTo.id) LEFT JOIN users AS UserFrom ON (Friend.user1_id = UserFrom.id) WHERE UserFrom.nick_name = 'shyam' AND Friend.status = 'friend' GROUP BY UserTo.id ORDER BY Friend.id desc LIMIT 12
1.First count query returning count properly
2.Second query if i put limit it not working properly. i.e there are 144 records in my table i need to display 12 per page.. only first page coming if i use group by and limit
You should have either DISTINCT or GROUP BY, not both - they're duplicating each other and probably causing problems with your query. Remove one of them and retry.
Note: If you want to manually request pages other than 1st with $this->paginate(), add 'page' parameter with page number to your $this->paginate array, like this:
$this->paginate = array_merge($this->paginate, array('page' => $pageNumber);
However, normally you wouldn't want to do this, as paginator does this automatically (passing page variable via URL parameter to your action) as long as you use pagination controls in your view (see http://book.cakephp.org/view/1233/Pagination-in-Views).
I'm trying to execute this query to fetch 'Banners' between a date_start and a date_end.
$current_date = date('Y-m-d');
$banners = $this->Banner->find('all',
array('conditions' =>
array("date_start >= " => $current_date,
"date_end <= " => $current_date)
));
I've attempted to use NOW() which seems to cause problems, I've tried using an "AND" condition and I've also concatenated the query with $current_date (eg. "date_start =>".$current_date)
Any ideas where I'm going wrong?
Edit
Managed to get it working by switching the conditions around:
$banners = $this->Banner->find('all', array('conditions' => array("'$current_date' >=" >= "date_start", "'$current_date' <=" => 'date_end')));
you should change your equation
date_start <= $current_date and
date_end >= $current date
lets say date_start is 1/5/2011
and date_end is 3/5/2011
and curr_date is 2/5/2011
notice that curr_date is bigger than start_date and smaller than end_date, in your condition you check the opposite