Tom
Tom

Reputation: 336

How to pass custom mysql query in $this->paginate() in cakephp?

I'm facing this problem as a CAKEPHP beginner, sorry if this is a very no-brainer question.

I have a TIMESTAMP field in one of the table in my database, but I only want to show the Month & Day from the database. As there is a mysql function MONTHNAME() & DAYOFMONTH(), this would be the solution for me, but I was wondering how do I do this using the paginate() function as shown below.

In my LogController index(), I have the following code;

$this->Log->recursive = 0;
$this->paginate = array(
'limit' => 10,
'order' => array(
'Log.id' => 'desc'
));

$this->set('logs',$this->paginate());

And in my View/Logs/index.ctp, I have;

$i = 0;
foreach ($logs as $log):
echo h($log["Log"]["time"]);

How do I do this?

Upvotes: 2

Views: 596

Answers (2)

Anh Pham
Anh Pham

Reputation: 5481

echo date('M d',$log["Log"]["time"]);

Upvotes: 1

Hans Wassink
Hans Wassink

Reputation: 2577

Add fields to your paginate function.

     $this->paginate = array(
        'limit' => 10,
        'order' => array(
            'Log.id' => 'desc'
        ),
        'fields' => '*,MONTHNAME(Log.time)');

Upvotes: 2

Related Questions