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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:33:10+00:00 2026-05-18T09:33:10+00:00

Let’s say we have a User Model that has many Posts. The Posts Model

  • 0

Let’s say we have a User Model that has many Posts.

The Posts Model has many Categories

The Posts Model also has many Comments.

How do we find dynamically, the relationships that the User Model has?

The idea is to make an admin backend to a site, where I can have one function, that when passed a model, can retrieve all data related to that Object, and show it based on the found data’s relationship.

I’m guessing I need to access the Model Itself, and not an instance.

Any help would be appreciated, thank you.

  • 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-18T09:33:11+00:00Added an answer on May 18, 2026 at 9:33 am

    I Finally did this, so here’s the code I used if anyone else wants it.

    I make no claims to being a good programmer, I’m pretty new.

    If someone actually was going to use this, and needed some help with my logic, I’ll comment it out fully, but I’m running on empty tonight…

    public function get_child_objects($recursive = 0, $init = FALSE)
    {
        if ( ! $init )
        {
            Session::instance()->delete('model_register');
            $init = TRUE;
            $model_register = array();
        }
        else
        {
            $model_register = Session::instance()->get('model_register');
        }
    
        $return_array = array();
    
        $parent_id = $this->id;
        $parent_type = $this->_object_name;
    
        // prevent unending loops of many to many relationships
        if ( ! isset($model_register[$this->_object_name]) )
        {
            $model_register[$this->_object_name] = TRUE;
        }
        else
        {
            return;
        }
    
        Session::instance()->set('model_register', $model_register);
    
        if ( ! count($this->_has_many))
        {
            return;
        }
    
        // foreach _has_many relationship get objects
        foreach ($this->_has_many as $child_object_type => $child_object_data) {
            $child_object_type_singular = Inflector::singular( (string) $child_object_type);
            $child_object_type_plural = Inflector::plural( (string) $child_object_type);
    
            $many_to_many = FALSE;
            if (isset($child_object_data['through']))
            {
                $many_to_many = TRUE;
            }
    
            if (isset($child_object_data['model']))
            {
                $child_object_type = $child_object_data['model'];
            }
    
            if ( ! $many_to_many)
            {
                $child_objects = ORM::factory($child_object_type_singular)
                            ->where($parent_type.'_id', '=', $parent_id)
                            ->find_all();
    
                if ( ! count($child_objects))
                {
                    continue;
                }
            }
            else
            {
                $child_object_type_plural = Inflector::plural( (string) $child_object_type);
                $child_objects = $this->$child_object_type_plural->find_all();
    
                if ( ! count($child_objects))
                {
                    continue;
                }
            }
    
            $obj_arr = array();
            foreach ($child_objects as $child_object) 
            {
                if ( (bool) $recursive)
                {
                    $recursive = $recursive - 1;
                    $obj_arr[$child_object->id] = $child_object->get_child_objects($recursive, $init);
                }
                else
                {
                    $obj_arr[$child_object->id] = NULL;
                }               
            }
            $return_array[$child_object_type_plural] = $obj_arr;
    
    
        } // foreach
    
        return $return_array;
    } // get_child_objects
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have an facebook application running using the JS SDK. First user
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say you have a method that expects a numerical value as an argument.
Let's say I have a bunch of links that share a click event: <a
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say that I have a set of relations that looks like this: relations

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.