Introduction to QQuery
The querying logic behind all the load() methods in your ORM classes is powered by QQuery, or QQ for short. Put simply, QQ is an object oriented API to perform SELECT-based queries on your database and that returns PHP variables or ORM objects.
While the ORM classes utilize basic, straightforward SELECT statements in its Load methods, QQ is capable of much more complex queries.
At its core, any QQ query will return a collection of objects of the same type (e.g. a collection of Person objects). But the power of QQ is that we can branch beyond this core collection by bringing in any related objects, performing any SQL-based clause (including WHERE, ORDER BY, JOIN, aggregations, etc.) on both the core set of Person rows and any of these related objects rows.
Every code generated class in your ORM will have the three following static QQuery methods:
- querySingle(): to perform a query to return just a single object (typically for queries where you expect only one row)
- queryArray(): to perform a query to return just an array of objects
- queryCount(): to perform a query to return an integer of the count of rows (e.g. "COUNT (*)")
All three QCubed Query methods expect two parameters, a Condition and an optional set of Clauses. Conditions are typical conditions that you would expect to find in a SQL WHERE clause, including Equal, GreaterThan, IsNotNull, etc. Clauses are additional clauses that you could add to alter your SQL statement, including methods to perform SQL equivalents of JOIN, DISTINCT, GROUP BY, ORDER BY and LIMIT.
Both Condition and Clause objects will expect Node objects as parameters. Nodes are objects that represent SQL tables, individual columns within a table, or association tables. Node classes for your entire ORM is code generated for you.
The next few examples will examine all three major constructs (Node, Condition and Clause) in greater detail.
Notice that QCubed Query doesn't have any construct to describe what would normally be your SELECT clause. This is because we take advantage of the code generation process to allow QQuery to automagically "know" which fields that should be SELECT-ed based on the query, conditions and clauses you are performing. This will allow a lot greater flexbility in your data model. Because the framework is now taking care of column names, etc., instead of the developer needing to manually hard code it, you can make changes to columns in your tables without needing to rewrite your QQuery calls.
QuerySingle Example
John Doeooo
QueryArray Example
- Alex Smith
- Wendy Smith
- Samantha Jones
QueryCount Example
3 rows.