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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:16:55+00:00 2026-06-06T14:16:55+00:00

If I run a query simialr to: SELECT * FROM user u LEFT JOIN

  • 0

If I run a query simialr to:

SELECT * FROM user u
LEFT JOIN orders o ON o.user_id=u.id
LEFT JOIN payments p ON p.order_id=o.id

Resulting in data like:

u.id, u.name, o.id, o.item, o.date_time, p.id, p.amount, p.date_time
  99    jeff    17  spring   12-12-2012    12     10.99   13-12-2012
  99    jeff    18     jam   12-12-2012    13       .99   16-12-2012
  99    jeff    19     car   22-12-2012    14      1000   17-12-2012
  99    jeff    19     car   22-12-2012    15      1000   18-12-2012
  99    jeff    19     car   22-12-2012    16      1000   19-12-2012

So user Jeff has 3 orders, he has made 3 payments towards his car.

Give the PHP objects User, Order and Payment and a single DB query – how do they get hydrated? I would like to see pseudo-code, actual php code or the pattern name I should read up on 🙂

  • 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-06T14:16:57+00:00Added an answer on June 6, 2026 at 2:16 pm

    Because no answers, I’ll try to describe how I solved this. This is a toy solution.

    Queries to the database are done in a little language I called QQL, this was inspired by Doctrine 2 DQL.

    SELECT *
    FROM user
    JOIN user order
    JOIN order payment
    WHERE user.name=?
    

    This gets parsed and an SQL statement can be built. The parse tree is also used to map the result set back to an object graph.

    Each model has a description of ‘relations’ to other models. So User has a one-to-many to Order, Order has a one-to-many to Payment. The description is in an array containing the model name, the primary key and foregin key names.

    To build the JOIN:

    JOIN user order
    

    Inspect the User model, get the table name ‘users’, then find the relationship ‘order’, get the Order table name ‘orders’ and joining keys. Use this to build:

    JOIN orders o ON o.user_id=users.id
    

    Once the query has run and results are returned to build the object graph. What I did was get all the distinct models used in the query (in this case User, Order and Payment) then for each row Hydrate each one:

    // query DB and get results into an array called $rows
    foreach ($rows as $row) {
        foreach (array('User', 'Order', 'Payment') as $model) {
            $o = new $model;
            $o->hydrate($row);
            // inspect primary key - have we got this object already? store or throw away
        }
     }
    

    My hydrate method was extremely thin (quick) because a lot of the objects that are built will be duplicates and get removed. From the result set in my question you can see that User(‘Jeff’) will be built 5 times, 4 of which are duplicates and will be discarded.

    Once the results have been read, there are 3 lists of objects. Users, Orders and Payments. These are passed to a Graph Builder along with the parse tree.

    The Graph Builder uses the parse tree to look at the relationships. Starting at the ‘root’ model (determined by the “FROM user”), inspect the parsed QQL to find the JOIN requested (User->id TO Order->user_id) adds the Orders to the User->orders array. It then does the same with (Order->id TO Payment->order_id).

    The result is:

    $user->name == 'jeff'
    $user->orders[0]->item == 'spring'
    $user->orders[1]->item == 'jam'
    $user->orders[2]->item == 'car'
    $user->orders[2]->payments[2]->date_time == '19-12-2012'
    

    I ended up with four main classes, the ORM, a Model_Base that all models extend (this makes sure each model has a ‘table name’, ‘columns’ and ‘relations’), a QQLParser and a Graph Builder. The ORM class was by far the largest at nearly 200 lines.

    Conclusion. It works. It feels similar to Doctrine 2, (so my eventual transition to using Doctrine 2 will be less painful). It could be more efficient. Results of profiling a test page reading in a few thousand objects, by far the slowest parts were the SQL query (3ms) and freeing the mysqli results (1ms) including the model classes (0.7ms). Writing it was fun and only took a day.

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

Sidebar

Related Questions

If I run the following query: select * from mysql.user order by abcdef; MySQL
When I run the query : select count(*) from (select idCover from x90..dimCover group
If I run a query such as: SELECT COUNT(*) as num FROM table WHERE
How can run sql query CoreData similar this: SELECT COUNT(*) FROM headphones WHERE id
When I run the query: select (100/50) It give me 2 - good. But
If I try to run this query in SQL Server 2005: SELECT 1 WHERE
I'm trying to run a query from a .NET page but I seem to
I wish to run an SQL select statement similar to this SELECT * FROM
When I execute a simple SELECT * FROM... query (seven columns) in Management Studio
Here's my query: SELECT solGroup,; SUM(IIF((; SELECT COUNT(*) FROM cgift c2; WHERE c2.solgroup !=

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.