Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:12:43+00:00 2026-06-07T05:12:43+00:00

I extended the CakePHP Blog Tutorial, and added Categories for my Posts. The Posts

  • 0

I extended the CakePHP Blog Tutorial, and added Categories for my Posts. The Posts Model belongsTo the Category Model. In my Posts View I am looping threw my Categories table to list the Categories for a Menu in the View, which works fine:

/* gets the category names for the menu */
$this->set('category', $this->Post->Category->find('all'));

Now I am trying to add the Post count to each Menu (Category) Item. So far I got this:

/* gets the category count for category 2*/
$this->set('category_2_count', $this->Post->find('count', array(
'conditions' => array('Category.id =' => '2'))));

The Problem is that I obviously can’t use the Loop in my View anymore. With this I have to get each Category + each Count, which seems very inelegant. Is there a way to query the Category Names and the Count and get one Array for the View?

Any Ideas? I am new to Cake and any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:12:44+00:00Added an answer on June 7, 2026 at 5:12 am

    In your controller:

    $this->set('category', $this->Post->Category->find('all', array(
        'fields' => array(
            '*',
            '(SELECT COUNT(*) FROM posts WHERE category_id = Category.id) AS `post_count`'
        )
    )));
    

    In your view:

    foreach ($category as $c) {
        echo $c['Category']['name']; // category name
        echo $c[0]['post_count']; // post count
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I extended the User model in django to include several other variables, such as
I extended the User model in Django by using Profiles. As you know, creating
I have succesfully extended a view and overridden the onDraw() method I can draw
I extended a View and call the invalidate() method to force redrawing. I only
I have extended the android View class, and now I want to define a
I have extended the pages table with one text field named TypoScript . My
I have one extended Data Table in payeeSearch.xhtml displayed as - 1) After clicking
I've extended Django's contrib.auth.models user like: class UserProfile(models.Model): user = models.ForeignKey(User, related_name = 'profile')
I extended my User Model as described in this SO Posting: Extending the User
In the sun extended RMI tutorial, they have some interesting code which implements a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.