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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:49:41+00:00 2026-05-28T01:49:41+00:00

How to make model, which connects three tables from a database? My DB structure

  • 0

How to make model, which connects three tables from a database? My DB structure is:

  • users
    • id
    • other fields
  • users_info
    • id
    • users_id – foreign key to ‘users’ table
    • other fields
  • users_credentials
    • id
    • users_id – foreign key to ‘users’ table
    • other fields

Former, I wanted to have 2 tables bonded in one model, so I used addRelatedEntity method. Here is my code:

class Model_UserInfo extends Model_Table{
    public $entity_code = 'user_info';
    function init(){
        parent::init();
        $this->addField('phone')->caption('Nr tel');
        $this->addField('users_id');
    }
    function addRelation(){
        $this->addRelatedEntity('i','users','users_id','left outer');
    }

}

and then I extend it in other file –

class Model_User extends Model_UserInfo{
    function init(){
        parent::init();
        parent::addRelation();

        $this->newField('name')->caption('Imie')->mandatory(true)->relEntity('i','name');
    }

}

And that worked great. But what if I want to bond 3 tables into one model? I cannot extend 2 classes. Is there a way to use addRelatedEntity in Model_User, and have references to Model_UserInfo and Model_Credentials?

  • 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-28T01:49:41+00:00Added an answer on May 28, 2026 at 1:49 am

    You would need to probably reverse the joins and start with the “user” table joined by 2 other tables, although it does not matter that much as long as you get the right query.

    First, add $this->debug(); into initialization of your model. That will help you a lot when you will be debugging joins, updates etc.

    class Model_User extends Model_Table {
        public $entity_code='users';
        function init(){
            parent::init();
            $this->newField('name')
                ->caption('Imie')
                ->mandatory(true);
        }
    }
    
    class Model_User_Combined extends Model_User {
        function init(){
            parent::init();
    
            $this->addRelatedEntity('info','users_info','inner','related');
            $this->addRelatedEntity('cred','users_credentials','inner','related');
    
            $this->addField('phone')->caption('Nr tel')->relEntity('info');
            $this->addField('password')->caption('Parol')->relEntity('cred');
        }
    }
    

    Compared to the solution you have right now, this uses “users” as master table and joins additional tables as “related”.

    Also you will benefit from inheritance, and Model_User will add some basic user fields for you.

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

Sidebar

Related Questions

I'm trying to generate my database tables from my Entity Framework model, but I'm
I have a user model on which I check to make sure that the
I have an application A with a domain-model which is mapped to a database
I have a Message model which has a create date. How can I make
I'm writing a view model which derives from ReactiveValidatedObject . This allows me to
Which is the strictest mode of XHTML to make my coding habits semantically and
If I have the following: {hdrs: [Make,Model,Year], data : [ {Make:Honda,Model:Accord,Year:2008} {Make:Toyota,Model:Corolla,Year:2008} {Make:Honda,Model:Pilot,Year:2008}] }
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
I am setting up DJango admin to make a model editable. On the same
I'm trying to make an application that keeps an object model in sync with

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.