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 9185671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:20:02+00:00 2026-06-17T19:20:02+00:00

Long story short, my database is set up as follows: Category hasMany Topic Topic

  • 0

Long story short, my database is set up as follows:

Category hasMany Topic

Topic belongsTo Category

Topic hasMany Section

Section belongsTo Topic

Section hasMany Subsection

Subsection belongsTo Section

In my Categories controller I can easily get all my information using a simple find query. However, in my subsections controller for example, I would like to do a complex find query such as:

$conditions = array(
        'Subsection.title' => $subsectionname,
        'Section.title' => $sectionname,
        'Topic.title' => $topicname,
        'Category.title => $categoryname
);

$subsection = $this->Subsection->find('first', array(
        'conditions' => $conditions, 
        'recursive' => 3,
));

However, I can only get data about the Subsection and the Section from the query this way. I can access the subsection’s Category in the View by using a call such as $mysubsection['Section']['Topic']['Category']['title']; but I’d ideally like to have all the filtering done in one call. Is this possible?

  • 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-17T19:20:04+00:00Added an answer on June 17, 2026 at 7:20 pm

    Figured out a way of doing this. Specifically, I wanted to use really nice looking URLs, so that a user could type in something like http://example.com/aCategoryTitle/aTopicTitle/aSectionTitle/aSubsectionTitle – but this data needs to be validated so that the subsection definitely belongs in the correct category, topic etc..

    $conditions = array(
            'Subsection.title' => $subsectionname,
            'Section.title' => $sectionname,
        );
    
    $subsection = $this->Subsection->find('all', array(
            'conditions' => $conditions, 
            'recursive' => 2,
        ));
    

    This is a simple find call which matches all subsections with the correct title and section title.

    if (!$subsection) {
            // nothing was returned
            $this->redirect(array('controller' => 'KnowledgeBase', 'action' => 'index'));
        }
    

    If there is no subsection matching that criteria, the user is redirected. However, there still may be multiple subsections found with the same section title. Take for example the following URLs:

    http://example.com/Cats/Feeding/Food/Meat and http://example.com/Dogs/Feeding/Food/Meat

    With these two examples, there would be multiple subsections found which match the above criteria. So we need to carry out some further validation:

    foreach ($subsection as $asubsection) {
            if (($asubsection['Section']['Topic']['Category']['title'] == $categoryname) &&
                ($asubsection['Section']['Topic']['title'] == $topicname) &&
                ($asubsection['Section']['title'] == $sectionname) &&
                ($asubsection['Subsection']['title'] == $subsectionname)) {
                    // correct parameters
                    // this variable can now be passed to the view
                    $this->set('mysubsection', $asubsection);
                } else {
                    // incorrect parameters in URL
                    $this->redirect(array('controller' => 'KnowledgeBase'));
                }
        }
    

    This loops through all the Subsections we’ve found, and ensures that their titles, and those of the section, topic and category they belong to, match the URL (as given by $this->request->params).

    Probably not the tidiest way of doing it, but it works, and works nicely with my routing. Always happy to take any further suggestions on board, but for now this is working well for me.

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

Sidebar

Related Questions

Long story short, the database I'm using needs to get looked at. Until that
Well long story short, my application needs to connect to the database in the
Long story short, I took over a project and a table in the database
To cut a long story short: As part of an online database access system
Long story short, we found files promoting prescription drugs on our server that we
Long story short, I have an ASP.NET application I'm trying to debug and at
Long story short, I'm developing a theme template for a blog that enables you
Long story short is I tried to quickly update a single row in SQL
Long story short, I'm making a custom Swing component that's basically a JTable with
Long story short, client's hosting is using php 5.2.5 and i desperately need to

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.