1

I am writing an app in CakePHP and need to get a count of posts for an image. its a one (image) to many (comments) link.

I have tried the following but it doesnt work as it cant find the table? I know I have managed to do this with just SQL queries in previous applications.

This is the code

$data = $this->Image->find('all', array(
'fields' => array('COUNT(Comment.comment_id) AS total', 'Image.*'),
'group' => array('Image.logo_id')
));

basically I want to output the image details and then a comment count which is associated to the image.

Hope someone can help.

Thanks.

6
  • 1
    Try setting debug level to 2 in the config file and see what happens. CakePHP should stop caching the structure of your database. Commented Sep 28, 2011 at 20:25
  • 2
    Have you looked at counterCache? Commented Sep 29, 2011 at 9:22
  • @Mike I have been able to do it the other way around as in count from the Comment table. Debug is set to L2, but it says it cant find the table Comment. Commented Sep 29, 2011 at 10:34
  • @deizel, yes I have seen that, but didnt think it was a brilliant solution... Commented Sep 29, 2011 at 10:35
  • How so? It's built specifically for this purpose. Admittedly it can't handle complex joins, but this is quite a simple requirement and should solve your problem nicely. Commented Sep 30, 2011 at 8:34

1 Answer 1

1

counterCache seems to be the answer, thanks guys!

Sign up to request clarification or add additional context in comments.

1 Comment

Please mark the answer as accepted so that this question doesn't show up in the unanswered questions list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.