CakePHP counterCache with contain

82 views Asked by At

i have the following database structure:

Users

id, username, ..., silver_medal_counter, gold_medal_counter, bronze_medal_counter

Badges

id, name, description, points, medal (silver, gold, bronze)

Userbadges

id, user_id, badge_id, created

If a userbadge were added, the counterCache should update the relevant medal_counter field in the users table.

What i have:

class Userbadge extends AppModel {
     public $belongsTo = array(
          'User' => array(
            'counterCache' => array(
                'silver_medal_counter' => array('Userbadge.Badge.medal' => 'silver'),
                'gold_medal_counter' => array('Userbadge.Badge.medal' => 'gold'),
                'bronze_medal_counter' => array('Userbadge.Badge.medal' => 'bronze')
            )
          ),
          'Badge' => array('className' => 'Badge')
      );
}

Note: The $actAs = array('Containable'); is included. I am using cakephp 2.5.6.

How can i solve this?

0

There are 0 answers