Cake is stuck in query setup.(I want to display the latest date of the month.)

Asked 2 months ago, Updated 2 months ago, 2 views

Hello, I'm a beginner at cake.I have a question because I am stuck with the query settings.

Here's what you want to do:

Displays the most recent date of the month.

I am writing a custom query as follows:

$this->paginate=[
        'limit' = > 20,
        'order'=>array('date'=>'desc'),
        'group' = > ['date' = > 'month(date)' ]
    ];

$datasList=$this->paginate($this->model);

The results are as follows:

Date
November 02, 2016 (smallest date)
October 01, 2016 (smallest date)
September 21, 2016 (smallest date)

What do you want to do

Date
November 05, 2016 (date is up to date)
October 05, 2016 (date is up to date)
September 30, 2016 (date is up to date)

What do you want to hear

·Whether or not the latest date can be displayed successfully just by setting the paginate.
·Is there any other better way?

Thank you for your cooperation.

php mysql cakephp

2022-09-30 14:12

1 Answers

The solution is as follows:
Implemented in the query builder
To set max(date), get query and set select.

$query=$this->Table->find();// query retrieval
$query->select([]
Specify 'date'=>$query->func()->max('date')//max(date)
]);
$this->paginate=[
        'limit' = > 20,
        'order'=>array('date'=>'desc'),
        'group' = > ['date' = > 'month(date)' ]
];
$datasList=$this->paginate($query);

Reference
https://teratail.com/questions/48872

http://book.cakephp.org/3.0/ja/orm/query-builder.html#sql

I couldn't solve myself.


2022-09-30 14:12

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.