Early Binding: Using Object Expansion and SQL Joins
When you need to perform loadAll() or loadArrayBy...() calls, and would like to include related objects in order to reduce the number of database calls in an extended query, you can use QCubed's Object Expansion functionality to specify which foreign key columns that you want to expand immediately.
The Object Expansion function, which is generated into each object in the ORM, will bind these related objects when the objects are initially created, thus the term "early binding".
In our example here, we will perform the exact same task as the previous example, pulling all the Project objects and displaying each object's ManagerPerson. Note that the only difference in our code is that we've added a QQ::expand() clause. There is no other difference with the way we access the restored objects and their related objects.
The end result is that instead of displaying the data using 5 queries, we have now cut this down to just 1 query. QCubed accomplishes this by using a LEFT JOIN in the SQL which is executed by the code generated ORM. If you are familiar with SQL, you can think of a QQ::expand() as the way to create JOINs in the resulting SQL.
Since the way we access the objects is the exact same, this kind of optimization can be done after the page is functional and complete. This follows the general philosophy of QCubed, which is to first focus on making your application functional, then focus on making your application more optimized. Often engineers can get bogged down making an application as optimized as possible before knowing what they actually want the software to do or testing it with real people, and in doing so they can waste time over-engineer some pieces of functionality. If the focus is on getting the application functional, first, then after the application is in a usable state, you can profile the functionality that tends to get used more often and simply focus on optimizing this smaller subset of heavily-used functionality.
You can expand from one table to another through foregin keys that reference a field in another table, through reverse references that have foregin keys that refer from the other table back to the table you are working with, or through association tables. Some of the other examples in this tutorial will teach you more about using each of these kinds of references.
List All Projects and its Manager
ACME Website Redesign is managed by Karen WolfeState College HR Systzzzz is managed by Mike Ho
Blueman Industrial Site Architecture is managed by John Doeooo
ACME Payment System is managed by Karen Wolfe