Skip to content

monsat/addition

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

Addition Behavior

This is a CakePHP's Behavior. You can use it to count up DB field's value ( without using counter cache )

Require

  • CakePHP2.0 or higher (2.0 branch)
  • MySQL (integer field)

API

sum(int $id, mixed $values = 1, array $fields = array())

Return type : boolean True on success, false on failure ( result by Model::updateAll() )

USAGE

# bootstrap.php
CakePlugin::load('Addition'); // or CakePlugin::loadAll();

# AnyModel
class User extends AppModel {
  public $actsAs = array('Addition.Addition' => array('fields' => array('views'))); // this option is default
  
  public countUp($id, $inc = 1) {
    $this->sum($id, $inc); // or $this->sum($id) ... to increment views field value
  }
}
# AnyController
public function counter($id = null) {
  // +2 (default fields [views])
  $this->User->sum($id, 2);
  
  // +1 (field [views]) and  +1 (field [another])
  $this->User->sum($id, 1, array('views', 'another'));
  
  // +1 (field [first]) and  +2 (field [second]) and  +3 (field [third])
  $this->User->sum($id, array(1, 2, 3), array('first', 'second', 'third'));
  
  // +1 (field [first]) and  +2 (fields [second, third, forth])
  $this->User->sum($id, array(1, 2), array('first', 'second', 'third', 'forth'));
}

decrement

$this->sum($id, -1);

About

CakePHP's addition behavior

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages