Hello guys,

I'm trying to use countercache to count the number of visible comments
a post has. This means that the count should only be added when the
active field is set to 1 (it's a sort of comment moderation system),
it's not a problem to set that up using counterScope.

So of course I've added a function to approve comments, the code is
below:
part of comments_controller.php:

        function admin_approve($post_id,$id)
        {
                //tried to set the post id with this, but no luck..
                $this->Comment->id = $id;
                $this->Comment->post_id = $post_id;
                $this->Comment->Post->id = $post_id;
                //save the visible field to approve it, counterCache should be
handled automatically.
                if($this->Comment->saveField('visible',1))
                $this->Session->setFlash('This comment has been approved');
                $this->redirect(array('controller' => 'comments', 'action' =>
'admin_index'));
        }


Unfortunately it doesn't work properly, the post is approved but
counter cache doesn't update and throws some error messages :(, it
seems to post_id is undefined.. I've tried setting it but that didn't
change anything, I hope you can do something with the error code:

Notice (8): Undefined index:  post_id [CORE\cake\libs\model\model.php,
line 1346]

Code | Context

$keys   =       array(
        "visible" => 1,
        "modified" => "2008-04-08 18:27:51"
)
$created        =       false
$assoc  =       array(
        "counterCache" => "comment_count",
        "counterScope" => array(
        "visible" => 1
),
        "className" => "Post",
        "foreignKey" => "post_id",
        "conditions" => "[empty string]",
        "fields" => "[empty string]",
        "order" => "[empty string]"
)
$parent =       "Post"

                }
                if ($this->{$parent}-
>hasField($assoc['counterCache'])) {
                    $conditions = array($this-
>escapeField($assoc['foreignKey']) => $keys[$assoc['foreignKey']]);

Model::updateCounterCache() - CORE\cake\libs\model\model.php, line
1346
Model::save() - CORE\cake\libs\model\model.php, line 1243
Model::saveField() - CORE\cake\libs\model\model.php, line 1105
CommentsController::admin_approve() - APP\controllers
\comments_controller.php, line 86
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 265
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 237
[main] - CORE\public_html\index.php, line 93

and a second error, pretty much the same?

Notice (8): Undefined index:  post_id [CORE\cake\libs\model\model.php,
line 1352]

Code | Context

$keys   =       array(
        "visible" => 1,
        "modified" => "2008-04-08 18:27:51"
)
$created        =       false
$assoc  =       array(
        "counterCache" => "comment_count",
        "counterScope" => array(
        "visible" => 1
),
        "className" => "Post",
        "foreignKey" => "post_id",
        "conditions" => "[empty string]",
        "fields" => "[empty string]",
        "order" => "[empty string]"
)
$parent =       "Post"
$conditions     =       array(
        "`Comment`.`post_id`" => null,
        "visible" => 1
)

                    $this->{$parent}->updateAll(
                        array($assoc['counterCache'] => intval($this-
>find('count', compact('conditions')))),
                        array($this->{$parent}->escapeField() =>
$keys[$assoc['foreignKey']])

Model::updateCounterCache() - CORE\cake\libs\model\model.php, line
1352
Model::save() - CORE\cake\libs\model\model.php, line 1243
Model::saveField() - CORE\cake\libs\model\model.php, line 1105
CommentsController::admin_approve() - APP\controllers
\comments_controller.php, line 86
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 265
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 237
[main] - CORE\public_html\index.php, line 93

and the queries:

        SELECT `User`.`id`, `User`.`username`, `User`.`password`,
`User`.`fname`, `User`.`lname`, `User`.`email`, `User`.`group_id`,
`User`.`active`, `User`.`created`, `User`.`modified` FROM `users` AS
`User` WHERE `username` = 'jelmer' AND `password` =
'69284a3bda59fd34c23065ccb06902d015bb7b7e' LIMIT 1              1       1       
1
2       SELECT COUNT(*) AS `count` FROM `comments` AS `Comment` WHERE
`Comment`.`id` = 85             1       1       1
3       UPDATE `comments` SET `visible` = 1, `modified` = '2008-04-08
18:27:51' WHERE `comments`.`id` = 85            1               1
4       SELECT COUNT(*) AS `count` FROM `comments` AS `Comment` LEFT JOIN
`posts` AS `Post` ON (`Comment`.`post_id` = `Post`.`id`) WHERE
`Comment`.`post_id` IS NULL AND `visible` = 1           1       1       1
5       UPDATE `posts` AS `Post` LEFT JOIN `categories` AS `Category` ON
(`Post`.`category_id` = `Category`.`id`) SET `Post`.`comment_count` =
0 WHERE `Post`.`id` IS NULL
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to