Without changing sql mode=only_full_group_by mode how can I execute group by in cakephp? - cakephp

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.

Related

union request and pagination in cakephp4

I made two requests. The first one gives me 2419 results and I store the result in $requestFirst. The second, 1 result and I store the result in $requestTwo.
I make a union :
$requestTot = $requestFirst->union($requestTwo);
The total of the $requestTot is 2420 results so all is well so far.
Then :
$request = $this->paginate($requestTot);
$this->set(compact('request'));
And here I don't understand, on each page of the pagination I find the result of $requestTwo. Moreover the pagination displays me :
Page 121 of 121, showing 20 record(s) out of 2,420 total
This is the right number of results except that when I multiply the number of results per page by the number of pages I get 2540. This is the total number of results plus one per page.
Can anyone explain?
Check the generated SQL in Debug Kit's SQL panel, you should see that the LIMIT AND OFFSET clauses are being set on the first query, not appended as global clauses so that they would affect the unionized query.
It will look something like this:
(SELECT id, title FROM a LIMIT 20 OFFSET 0)
UNION
(SELECT id, title FROM b)
So what happens then is that pagination will only be applied to the $requestFirst query, and the $requestTwo query will be unionized on top of it each and every time, hence you'll see its result on every single page.
A workaround for this current limitation would be to use the union query as a subquery or a common table expression from which to fetch the results. In order for this to work you need to make sure that the fields of your queries for the union are being selected without aliasing! This can be achieved by either using Table::subquery():
$requestFirst = $this->TableA
->subquery()
->select(['a', 'b'])
// ...
$requestTwo = $this->TableB
->subquery()
->select(['c', 'd'])
// ...
or by explicitly selecting the fields with aliases equal to the column names:
$requestFirst = $this->TableA
->find()
->select(['a' => 'a', 'b' => 'b'])
// ...
$requestTwo = $this->TableB
->find()
->select(['c' => 'c', 'd' => 'd'])
// ...
Then you can safely use those queries for a union as a subquery:
$union = $requestFirst->union($requestTwo);
$wrapper = $this->TableA
->find()
->from([$this->TableA->getAlias() => $union]);
$request = $this->paginate($wrapper);
or as a common table expression (in case your DBMS supports them):
$union = $requestFirst->union($requestTwo);
$wrapper = $this->TableA
->find()
->with(function (\Cake\Database\Expression\CommonTableExpression $cte) use ($union) {
return $cte
->name('union_source')
->field(['a', 'b'])
->query($union)
})
->select(['a', 'b'])
->from([$this->TableA->getAlias() => 'union_source']);
$request = $this->paginate($wrapper);

CakePHP how to get the running total in an entity using a virtual field?

I have field called deposit, I am trying to create a virtual field called balance. Below my desire output, It's like chaining sum.
deposit balance
100 100
300 400
10 410
I have tried below code in entity
public $balance = 0;
protected function _getBalance()
{
$this->balance = $this->balance + $this->deposit;
return $this->balance;
}
I have got all 0 in balance.
I am getting result like below
deposit balance
100 0
300 0
10 0
How can I get desire result ?
An entity has no idea about other entities, but that would be required in order for it to be able to sum up the balance.
The two solutions that come to my mind here are a) iterating over all the results and modifying the data, or b) in case your DBMS supports them, using window functions to create the running total on SQL level.
If you iterate over all results you can access the previous result's balance and calculate the sum and populate the balance field accordingly, for example in a result formatter:
$query->formatResults(function (\Cake\Collection\CollectionInterface $results) {
$previous = null;
return $results->map(function ($row) use (&$previous) {
if ($previous === null) {
$row['balance'] = $row['deposit'];
} else {
$row['balance'] = $previous['balance'] + $row['deposit'];
}
$previous = $row;
return $row;
});
});
On SQL level window functions would allow you sum up previous rows:
$query->select(function (\Cake\ORM\Query $query) {
return [
'deposit',
'balance' => $query
->func()
->sum('deposit')
->over()
->order('id')
->rows(null)
];
});
This would create a SELECT clause like this:
SELECT
deposit,
(
SUM(deposit) OVER (
ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)
) AS balance
where the sum is calculated over all previous rows up to and including the current row.
It should be noted that window functions on the builder are only supported as of CakePHP 4.1, in previous version you'd have to create custom expressions or pass raw SQL:
$query->select([
'deposit',
'balance' => 'SUM(deposit) OVER (
ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)'
]);
See also
Cookbook > Database Access & ORM > Query Builder > Adding Calculated Fields
Cookbook > Database Access & ORM > Query Builder > Window Functions

select count of records group by month in cakephp 3

I'm using CakePHP 3.x+
I have to show a graph on the page and thus want to build script for that.
I have to select count of records group by month for current year.
This is what I have tried.
$graph = $this->GenerateVideos->find()
->select('COUNT(id)', 'MONTH(created)')
->where(['YEAR(created)' => date('Y')])
->group(['MONTH(created)']);
which generates sql like
'sql' => 'SELECT GenerateVideos.COUNT(id) AS GenerateVideos__COUNT(`id`) FROM generate_videos GenerateVideos WHERE YEAR(created) = :c0 GROUP BY MONTH(created) ',
'params' => [
':c0' => [
'value' => '2018',
'type' => null,
'placeholder' => 'c0'
]
],
But this is giving error as
Error: SQLSTATE[42000]: Syntax error or access violation:
1064 You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '(`id`)
FROM generate_videos GenerateVideos WHERE YEAR(created) = '2018' GROUP BY' at line 1
Try using an array in your ->select() value:
->select(['COUNT(id)', 'MONTH(created)'])
In the book, it always shows an array, and it doesn't appear to be utilizing your second select value.
Or, per the book here, you could try this:
$query = $this->GenerateVideos->find();
$query->select(['count' => $query->func()->count('id'), 'month' => 'MONTH(created)']);
$query->where(['YEAR(created)' => date('Y')])
$query->group(['month' => 'MONTH(created)']);

cakephp check multiple tables

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

CAKEPHP Group by problem in paginate

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).

Resources