I was just looking for this!
How would one add a condition for the counter? e.g. only count articles if is_published = 1?
Thanks!
There're could be two choices, depends what do you mean:
* The behavior counts rows of the join table, e.g. articles_tags and conditions could be applied to this table if you had an extra field for example 'is_active' which means "is the association active?" in the join table.
* The second case is more difficult. You could have additional conditions in both models Tag and Article, for example you want to count tags for only active articles or count articles for only administrator approved tags. In this case count(*) must be done using join of each model depending on conditions and this leads to slow down and bad design.
Imagine if one wants to have count of tags of only approved articles and count of articles that have approved tags? Fortunately the second case can be "emulated" using first case, e.g. if you have unapproved article with assigned tags to it, just set "is_active" to "0" in the appropriate join table records, but you should handle this by yourself and that requires more coding from your side.
If you agree, I can easily implement conditions for the first case and update the behavior.
I think what he means is that he would prefer to use this the same way Cake's CounterCache behavior works. Meaning you set the behavior in the association, and optionally include a condition under which the counter should add/subtract to the total count. Say you have a Groups model and a Users model where each HABTM to the other.
You want your groups to count how many users that are not banned from the site belong to each group. With counterCache, you'd use counterScope and set it to array('User.banned' => 0) and that's it. My guess is that elven is looking for a similar behavior in your counterCacheHabtm.
So what he wants, basically, is this:
Model Class:
<?php
var $hasAndBelongsToMany = array(
'Group' => array(
'className' => 'Group',
'joinTable' => 'groups_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => '',
'counterCacheHabtm' => true,
'counterScopeHabtm' => array('User.banned' => 0),
),
);
?>
Personally, I think making this behavior work in as similar as possible a way to the original counterCache behavior is a very good idea, and I don't think it should take a whole lot of extra coding to do.
Hey man, excellent mod. I've been using something similar of my own.
Just a quick note; the link you're pointing people to download the package no longer exists, or is incorrect (http://plugins.jquery.com/project/MultipleFriendlyStarRating/). You should probably check into that and change your article accordingly.
This method has no impact on any other caching method, unless you tell Cake to use this by default, which you should not do.
When you create one or many cache configurations in bootstrap.php, you will need to tell Cake to use it else it would just use the default.
Thanks for clarifying that. I guess I should learn more about how Cake handles caching... :)
You should update your code to reflect Tyler's suggestion about encoding the URL properly.
Also, your hashing method is not taking into account the fact that Gravatar is expecting the email to be hashed in its lower-case form. Hence, the following change should be applied...
Change this:
Helper Class:
<?php
return Security::hash($email, $type);
?>
To this:
Helper Class:
<?php
return Security::hash(strtolower($email), $type);
?>
That's all there is to it. :)
I think what he means is that he would prefer to use this the same way Cake's CounterCache behavior works. Meaning you set the behavior in the association, and optionally include a condition under which the counter should add/subtract to the total count. Say you have a Groups model and a Users model where each HABTM to the other.
You want your groups to count how many users that are not banned from the site belong to each group. With counterCache, you'd use counterScope and set it to array('User.banned' => 0) and that's it. My guess is that elven is looking for a similar behavior in your counterCacheHabtm.
So what he wants, basically, is this:
Model Class:
<?phpvar $hasAndBelongsToMany = array(
'Group' => array(
'className' => 'Group',
'joinTable' => 'groups_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => '',
'counterCacheHabtm' => true,
'counterScopeHabtm' => array('User.banned' => 0),
),
);
?>
Personally, I think making this behavior work in as similar as possible a way to the original counterCache behavior is a very good idea, and I don't think it should take a whole lot of extra coding to do.
Just a quick note; the link you're pointing people to download the package no longer exists, or is incorrect (http://plugins.jquery.com/project/MultipleFriendlyStarRating/). You should probably check into that and change your article accordingly.
How would this affect Cake's own caching, such as view/element caching? Will Cake's methods still work, or is an ugly hack needed for that as well?
I'd love to use your ACL caching component, but if the Namespace Cache engine screws up the element caching than that might be a problem.
You should update your code to reflect Tyler's suggestion about encoding the URL properly.
Also, your hashing method is not taking into account the fact that Gravatar is expecting the email to be hashed in its lower-case form. Hence, the following change should be applied...
Change this:
Helper Class:
<?phpreturn Security::hash($email, $type);
?>
To this:
Helper Class:
<?phpreturn Security::hash(strtolower($email), $type);
?>
That's all there is to it. :)