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 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 that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have an arbitrary string like `A man + a plan
Let's say I have this: public DefaultListModel model = new DefaultListModel(); how do i
Let's say I have two entities: Physician Credentials And a physician can have many
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want 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.