Chris
Chris

Reputation: 361

cakephp paginate with group and group count

I have a db table with items with a matching group_id. I'd like to display those items in a paginator like this:

codeset_id - itemsInGroupCount
group1     -        3
group2     -        6

How can I do this? I have the distinct groups but how do I get the count?

My Controller:

public function admin_index() {
    $this->Codesetitem->recursive = 0;
    $this->paginate = array(
        'Codesetitem' => array(
            'group' => 'codeset_id',
            'order' => array('codeset_id' => 'asc')
        )            
    );
    $this->set('codesetitems', $this->paginate());
}

Upvotes: 0

Views: 3559

Answers (1)

Raffaele
Raffaele

Reputation: 20875

The following seems to work on my side:

public $components = array('Paginator');

public function index() {
  $this->Paginator->settings = array(
    'fields' => array('Item.group_id', 'COUNT(*) AS "Item.group_count"'),
    'limit' => 3,
    'group' => 'group_id'
  );

  $this->set('items', $this->Paginator->paginate('Item'));
}

I simply added COUNT(*) AS "Foo.bar" to the fields key.

Upvotes: 2

Related Questions