Code Monkey home page Code Monkey logo

cakephp-habtm-counter-cache-plugin's Introduction

CakePHP HABTM Counter Cache Plugin

A CakePHP plugin for Counter Cache functionality for HABTM associations

Details

Provides Counter Cache and Under Counter Cache (for hierarchical models) behavior with scope for HABTM associations e.g. Post habtm Tag,Category where Tag has a post_count and Category is a hierarchical and has post_count and under_post_count fields.

Under Counter Cache is a Counter Cache that counts all distinct records assigned to that node explicitly, or any of it's children. This is useful for figuring out how many posts are assigned to a category of one of it's children, which you can use to print out a nested list of categories with the number of posts next to them.

Usage example:

class Post extends AppModel {
  var $name = 'Post';
  var $actsAs = array('HabtmCounterCache');
  var $hasAndBelongsToMany = array('Category', 'Tag');
}

Requirements

  • CakePHP 1.3+ (I think) (Works with CakePHP v2.0)

Installation

git submodule add git://github.com/neilcrookes/CakePHP-HABTM-Counter-Cache-Plugin.git app/plugins/habtm_counter_cache

or download from http://github.com/neilcrookes/CakePHP-HABTM-Counter-Cache-Plugin

Add the counter cache fields to your database tables, e.g. for a tags table, add an integer field, default 0, called 'post_count'

Attach the behavior to the model whose records you're counting, e.g. your Post model (see configuration below for examples)

Configuration

Configuration is very flexible, for example:

  • Just attach and it will do counter caching for all hatbm associated models that have the counterCache field. E.g.

    var $actsAs = array('HabtmCounterCache');

  • Specify counterCache, counterScope and/or underCounterCache keys in the configuration options when you attach the behavior for these settings to be applied to all habtm associations. E.g.

    var $actsAs = array( 'HabtmCounterCache' => array( 'counterScope' => array('active' => 1) ), );

  • Introduce habtm association specific counterCache and counterScope settings by using the habtm alias as the key E.g.

    var $actsAs = array( 'HabtmCounterCache' => array( 'Tag' => array( 'counterCache' => 'weight' ) ), );

Copyright

Copyright Neil Crookes 2011

License

The MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.