Home N-13 News Forums Help Search
RegisterForgot password?
www.beatparty.biz kyhk
Help with News Pages Integration Please...! DreadManCandy

Latest N-13 News 4.0.2

What is N-13 News?
Where can I get help?

Network-13 Home
Old Posts

2011

  • CakePHP - Multiple counterCache fields on the same model
  • Time to bake a cake and rebuild the site
  • N-13 News 4.0.2 available for download.

2010

  • N-13 News 4.0 available for download
  • N-13 News 3.7 available for download.
  • N-13 News 3.6 available for download.
  • Integrating IntenseDebate Commenting
  • Integrating Disqus Commenting
  • N-13 News 3.5 available for download.

2009

  • N-13 News 3.4 available for download.
  • Translating N-13 News into other languages.
  • N-13 News 3.3 available for download.
  • Ideas & Suggestions for N-13 News 3.3
  • N-13 News 3.2 available for download.

2008

  • Domain transfer
  • Network-13 moving to a new host
  • N-13 News 3.1 available for download.
  • N-13 News 3.0 available for download.
  • N-13 News 3.0?
  • Updating the site design

2007

  • Free network-13 emails!
  • N-13 v5

2006

  • Small update
  • Moved servers
  • Design updated!
  • Register module updated
  • Forum updated
  • Network-13 v4
CakePHP - Multiple counterCache fields on the same model

Adding a counterCache field to your model if CakePHP is fairly straight forward, what may not be as straight forward is adding a second counterCache field to the same model.

Take this scenario, you have a standard forum, each forum category has a counterCache field to count the amount of threads for that category

<?php
class Thread extends AppModel {
    public $name = 'Thread';

    public $belongsTo = array(
        'SubForum' => array(
            'counterCache' => true,
            'foreignKey' => 'sub_forum_id',
            'className' => 'SubForum',
            'counterScope' => array('Thread.thread_id' => 0)
        )
    );
}

This is your standard counterCache implementation, Thread belongs to SubForum, the thread_count field in the SubForum model will automatically update every time a new thread is posted.

Now say wanted to add a second field to the SubForum model, a field to count the total threads with certain conditions, for example you want one counterCache field to count the total threads and a second to count the total threads & replies (total posts) to each of those threads.

<?php
class Thread extends AppModel {
    public $name = 'Thread';

    public $belongsTo = array(
        'SubForum' => array(
            'counterCache' => true,
            'foreignKey' => 'sub_forum_id',
            'className' => 'SubForum',
            'counterScope' => array('Thread.thread_id' => 0)
        ),
        'SubForum2' => array(
            'counterCache' => 'total_thread_count',
            'foreignKey' => 'sub_forum_id',
            'className' => 'SubForum',
            'counterScope' => array('Thread.thread_id <> 0'),
    );
}

Here we've said that the Thread has multiple relations to the SubForum, defining the second relation as SubForum2. The counterCache field in the second relation, instead of simply being set to true can be set to the column name you want to update.

0 Replies
2011-12-21 00:55:47


Network-13.com © 2011