PHPackages                             cwbit/cakephp-aggregate-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. cwbit/cakephp-aggregate-cache

ActiveCakephp-plugin[Caching](/categories/caching)

cwbit/cakephp-aggregate-cache
=============================

A Behavior plugin for CakePHP that extends the idea of counterCache and counterScope to more fields

82.5k3[3 issues](https://github.com/cwbit/cakephp-aggregate-cache/issues)PHP

Since Nov 3Pushed 11y ago3 watchersCompare

[ Source](https://github.com/cwbit/cakephp-aggregate-cache)[ Packagist](https://packagist.org/packages/cwbit/cakephp-aggregate-cache)[ RSS](/packages/cwbit-cakephp-aggregate-cache/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

cakephp-aggregate-cache
=======================

[](#cakephp-aggregate-cache)

=======================

A Behavior plugin for CakePHP that extends the idea of counterCache and counterScope to more fields.

Note: this is a git extension of the original AggregateCache behavior by Vincent Lizzi.

Installation
------------

[](#installation)

====

*\[Using [Composer](http://getcomposer.org/)\]*

Add the plugin to your project's `composer.json` - something like this:

```
{
	"require": {
		"cwbit/cakephp-aggregate-cache": "dev-master"
	}
}

```

Because this plugin has the type `cakephp-plugin` set in it's own `composer.json` composer knows to install it inside your `/Plugins` directory (rather than in the usual 'Vendor' folder). It is recommended that you add `/Plugins/AggregateCache` to your cake app's .gitignore file. (Why? [read this](http://getcomposer.org/doc/faqs/should-i-commit-the-dependencies-in-my-vendor-directory.md).)

*\[Manual\]*

- Download and unzip the repo (see the download button somewhere on this git page)
- Copy the resulting folder into `app/Plugin`
- Rename the folder you just copied to `AggregateCache`

*\[GIT Submodule\]*

In your `app` directory type:

```
git submodule add -b master git://github.com/cwbit/cakephp-aggregate-cache.git Plugin/AggregateCache
git submodule init
git submodule update

```

*\[GIT Clone\]*

In your `app/Plugin` directory type:

```
git clone -b master git://github.com/cwbit/cakephp-aggregate-cache.git AggregateCache

```

### Enable plugin

[](#enable-plugin)

In 2.0 you need to enable the plugin your `app/Config/bootstrap.php` file:

```
    CakePlugin::load('AggregateCache');

```

If you are already using `CakePlugin::loadAll();`, then this is not necessary.

Usage
-----

[](#usage)

The following was originally plagiarized from AggregateCache Behavior on the bakery. Modifications to the original text will be made as the plugin progresses

by vincentm8 on August 23, 2010

AggregateCache behavior caches the result of aggregate calculations (min, max, avg, sum) in tables that are joined by a hasMany / belongsTo association. I usually think of aggregates as being easy to calculate when needed, though in situations where the aggregate value is needed more often than the underlying data changes it makes sense to cache the calculated value. Caching the result of the aggregate calculation also makes it easier to write queries that filter or sort on the aggregate value. This behavior makes caching the result of aggregate calculations easy. AggregateCache is based on the CounterCache behavior (\[url\][http://bakery.cakephp.org/articles/view/countercache-or-counter\_cache-behavior\[/url\]](http://bakery.cakephp.org/articles/view/countercache-or-counter_cache-behavior%5B/url%5D)). To introduce the AggregateCache behavior let's use a posts and comments example. The date of the most recent comment, and the maximum and average ratings from each comment will be cached to the Post model, which will make it easy to use this information for display or as filters in other queries.

#### Posts table:

[](#posts-table)

```
CREATE TABLE `posts` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `created` datetime default NULL,
  `modified` datetime default NULL,
  `name` varchar(100) NOT NULL,
  `description` mediumtext,
  `average_rating` float default NULL,
  `best_rating` float default NULL,
  `latest_comment_date` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

#### Comments table:

[](#comments-table)

```
CREATE TABLE `comments` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `created` datetime default NULL,
  `modified` datetime default NULL,
  `name` varchar(100) NOT NULL,
  `description` mediumtext,
  `post_id` int(10) unsigned NOT NULL,
  `rating` int(11) default NULL,
  `visible` tinyint(1) unsigned NOT NULL default â€˜1â€™,
  PRIMARY KEY  (`id`),
  KEY `comments_ibfk_1` (`post_id`),
  CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

#### Post model:

[](#post-model)

```
