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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:07:48+00:00 2026-06-03T11:07:48+00:00

Hi I am using CodeIgniter with DataMapper and really need some help defining relationship

  • 0

Hi I am using CodeIgniter with DataMapper and really need some help defining relationship in a model class (DataMapper Model Class)

How would I write the model relation for this. Slightly confused about the Self relation, i.e menu item and sub menu

Menu has many submenus and submenus can have one or more sub-submenus

Class: navigation
Table: Navigation
[id] [parent_id] [Name] ..

Thanks

  • 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-03T11:07:53+00:00Added an answer on June 3, 2026 at 11:07 am

    This is a one-to-many relation (an item has one parent, a parent can have many items).

    So your model would look like:

    class Menu extends DataMapper {
    
        public $has_one = array(
            'parent' => array(
                'class' => 'menu',
            ),
        );
    
        public $has_many = array(
            'menu' => array(
                'class' => 'menu',
                    'other_field' => 'parent',
            ),
        );
    
    }
    

    This will allow you do do:

    // assume your tree root has parent id 0
    $root = new Menu();
    $root->where('parent_id', 0)->get();
    
    // get the first level menu from the root
    $submenu = $root->menu->get();
    
    // get the parent from the first submenu entry (should be root again)
    $rootagain = $submenu->parent->get();
    

    Note that (as I already replied on the CI forum) this is not a very optimal solution, as a tree can be several levels of nesting, with this setup it means having to iterate as you can only retrieve one level at the time, and for a single parent. This will become a nightmare for any size tree.

    Check out the nestedsets extension, which will allow you to build nested sets tree in a table, and adds the methods to Datamapper to manipulate those sets (like working with parents, children, syblings, inserting new records at specific locations in the tree, etc).

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

Sidebar

Related Questions

I need help constructing a CodeIgniter Datamapper ORM query that includes join fields. I
Using codeigniter and oci8 for a project. $this->db->insert_id(); would would perfect for getting the
i am using codeigniter. i have class Orders which with some methods like start_order(),
I am using the CodeIgniter DataMapper ORM, but there is something that I don't
I'm using CodeIgniter and I got a model that fetches let's say all recipes
I am using codeigniter and its pagination class. It works perfectly and it looks
I'm writing a CRUD system using CodeIgniter, implementing some 'one to many' dependencies between
I am using CodeIgniter's form validation . Here is an example: $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
Using CodeIgniter's Active Record class and MySQL, I have a table of posts with
using CodeIgniter normally one has to specify the controllers in the config/routes.php file. This

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.