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

  • Home
  • SEARCH
  • 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 7308253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:31:55+00:00 2026-05-28T23:31:55+00:00

EDIT – I was able to fix this problem using code from ‘Farray’ with

  • 0

EDIT – I was able to fix this problem using code from ‘Farray’ with the following changes:

$categories = $this->Category->find(
        'all',
        array(
        'fields' => array(
           'Category.id',
           'Category.title'
        ),
        'order' => 'Category.title ASC',
        'contain' => array(
            'Article' => array(
            'fields' => array(
                'Article.id',
                'Article.title',
                'Article.slug',
                'Article.main_image'
            ),
            'conditions' => array(
                'published' => 1
            ),
            'order' => 'Article.id DESC',
            'limit' => 4
            )
        )
        )
    );

==================================================================================

My app has an Articles controller as well as Categories controller. I want to display all my categories on the bottom of my Articles’ index and for each Category I want to display 4 articles per the following:

Category 1
    article 1
    article 2
    article 3
    article 4

Category 2
    article 1
    article 2
    article 3
    article 4

etc...

Currently I am accomplishing this using the following code in my index.ctp

foreach($categories as $category){
    echo $category['Category']['title'];
    $this->element('related_articles', array('categoryID'=>$category['Category']['id']));
}

Basically, I go thru all available categories and for each category in foreach

  1. I call an element to get related articles
  2. This element uses requestAction to call a Categories controller action which return related articles
  3. Once again, I go thru foreach to display all related articles

This process is repeated for every Category that I have. Obviously this is not the best way to do this. What is the best way?

The following is what I use in my Articles Controller view function for categories:

$categories = $this->Article->Category->find(
        'all',
        array(
            'fields' => array(
                'Category.id',
                'Category.title'
            ),
            'order' => 'Category.id DESC',
            'recursive' => 1,
            'limit' => 1
        )
    );

With this, my load time gets decreased considerably and if I can find another faster way that would be great…

  • 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-05-28T23:31:56+00:00Added an answer on May 28, 2026 at 11:31 pm

    Use the Containable model behavior and you should be able to do something like this:

    $this->Article->contain();
    $categories = $this->Article->Category->find( 'all', array(
        'contain' => array(
            'Category' => array(
                'fields' => array(
                    'Category.id',
                    'Category.title'),
                'order' => 'Category.id DESC' ),
            'Article' => array(
                'fields' => array(
                    'Article.id',
                    'Article.title'),
                'order' => 'Article.id DESC',
                'limit' => 4
                ) ) ) );
    

    This would get all categories, and 4 articles for each category. I haven’t touched 1.3 in a while so I might be slightly off on the syntax, but this is the way Cake likes to limit model fetches. If you use Containable a lot, you can set it in the model definition (or in AppModel if you want it to apply to all models).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
EDIT:This did it: SELECT DISTINCT profileid FROM profilesrelevation WHERE profileid NOT IN ( SELECT
EDIT: for those who come here with a similar problem, now i know this
EDIT How to simplify the following code: if(x(a) > x(b)) s = b; e
Edit: This is currenty kinda resolved, Instead of struggling with the problem, I started
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++
Edit: I have solved this by myself. See my answer below I have set

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.