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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:31:49+00:00 2026-05-13T13:31:49+00:00

This might be something very trivial, but I am a novice with some Object-Oriented

  • 0

This might be something very trivial, but I am a novice with some Object-Oriented patterns.

Put simply, is it a bad practice to use methods from one DAO in another DAO? I am trying to create an entity within a DAO and find it very hard to create that entity using just that DAO. So, is it ok to use other DAO methods within the other DAO?

Example:

public function readAllUsers() {
    $sql = "SELECT * FROM user";
    return $this->execute($sql);
}

public function execute($sql) {
$result = mysql_query($sql, $this->getDBConnection())
                or die(mysql_error());
    $user = array();
    if(mysql_num_rows($result) > 0) {
        for($i = 0; $i < mysql_num_rows($result); $i++) {
            $row = mysql_fetch_assoc($result);
            $user[$i]->setUsername(row["userName"]);
             ...set user info...
            $user[$i]->setAddresses($addressDAO->readAddressByUserId($userId));
        }
    }
    return $user;
}

NOTE: There are a good bit of attributes like this in the user entity that have a one-to-many relationship with the entity itself (addresses, emails, phone numbers, etc.). The query that would be needed (with all the linking tables that are used) would be tremendously complex.

Thanks,

Steve

  • 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-13T13:31:49+00:00Added an answer on May 13, 2026 at 1:31 pm

    No, I wouldn’t do it that way at all.

    Sounds like you want to model a one-to-many relationship, so your hope is to use the inner DAO to get the many objects and then map them into the one.

    Resist that temptation.

    Your code tells you why:

    1. The outer loop brings back N objects with a one-to-many relationship.
    2. The inner loop queries once for each of the N objects and brings back its dependencies.

    Classic noob N+1 query error. The network latency will kill you for large N.

    The right thing to do is to do one query that brings back all your data at once and map it into the object(s) in question.

    Maybe an ORM tool like Hibernate would be a good fit. If you have lots of 1:m relationships, I’d bet Hibernate would do as good as or a better job of generating efficient SQL than you will.

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

Sidebar

Related Questions

This might seem like a very easy question for some of you folks, but
This question might seem very specific but I am in need of some ideas
It's been some time since I've used JQuery, so this might be something very
This might be something very straight forward and I really think it should work
This might sound like a stupid question but just trying to learn something here.
This might be a very basic question but it confuses me. Can two different
This might be very basic question but I was wondering why can't I assign
Sorry this might be a very stupid question. But I did have bug with
Hi there this might me a very noob question but I'm stuck, and I'm
This might seem like a very easy answer but I cant seem to wrap

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.