• support@answerspoint.com

cakephp COUNT items per month in a year

3768

How do you use cakephp to count, for example the number of posts, made every month in a year?

Preferably using Model->find('count') and get the data in an array.

2Answer


0

I just did something similar, using only CakePHP (no direct queries). It works in CakePHP 2, haven't tested in 1.x.

The code for your example would be something like this:

$params = array(
  'recursive' => -1,
  'fields' => array('id', 'MONTH(created)')
  'group' => array('YEAR(created)', 'MONTH(created)')
);

$numberOfPosts = $this->Model->find('count', $params);
  • answered 8 years ago
  • G John

0

This comes close

Query $data = $this->Post->query("SELECT COUNT(id),MONTH(created) FROM posts GROUP BY YEAR(created), MONTH(created);");

Return

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [COUNT(id)] => 1
                    [MONTH(created)] => 3
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [COUNT(id)] => 2
                    [MONTH(created)] => 4
                )

        )

)
  • answered 8 years ago
  • B Butts

Your Answer

    Facebook Share        
       
  • asked 9 years ago
  • viewed 3768 times
  • active 9 years ago

Best Rated Questions