Purpose

In Wonderful Relations, Queries are a powerful and flexible layer for retrieving, joining, filtering, and transforming data from one or more database tables. Unlike traditional SQL queries that are written manually, queries in Wonderful Relations are built using a visual, structured interface. This makes it easier to compose, reuse, and manage queries even in complex applications.

Queries are used throughout the system—for DataTables, Template Objects, PDF Templates, aggregations, joins, and more. They enable dynamic, optimized, and maintainable data access patterns, while still being compatible with the MySQL syntax underneath.

One of the key advantages of this approach is the ability to reuse queries through modular design—subqueries, unions, and joins can reference existing queries without duplication. This allows for highly organized and efficient query management, especially in larger systems.

How It Works

Low Code

Queries are configured using an interactive interface rather than written as text. Each component—such as fields, joins, filters, or ordering—is defined in a structured UI. This allows for:

  • Better error handling and validation
  • Easier onboarding for non-SQL experts
  • Modular reuse of query parts

More Than SQL

While functionally similar to SQL, Wonderful Relations queries offer:

  • Reusable Subqueries(used in other queries or DataTables)
  • Aggregate Queries (for footer calculations in tables)
  • Placeholder Filters (replaced dynamically via Template Options)
  • Efficient Query Caching (automatically cached after first execution)
  • Visual Definition of Joins, Filters, and Groupings

Visual Example

Query GUI

The Query GUI in Wonderful Relations provides a visual interface for building and managing queries. Below is an example of an “Entity (List)” query shown in the visual editor. It demonstrates how fields, joins can be configured without writing raw SQL. This visual approach makes query building faster, reusable, and less error-prone.

Preview (Resulting Query)

Integration & Usage

In most cases, queries are used via the Wonderful Relations UI, where you can simply select them from a dropdown—e.g., in DataTables, Template Objects, or PDF Templates. This allows you to link existing queries without writing code.

However, queries can also be accessed programmatically via their identifier. This is useful when integrating queries into plugin logic, custom endpoints, or background processing.

Executing Queries in PHP

Queries can be executed directly via their identifier:

 
$executor = new Executor();
 
// Fetch multiple rows
$result = $executor->get_results('your_identifier', array( 'column' => $value ));
 
// Fetch one row
$row = $executor->get_row('your_identifier', array( 'column' => $value ));
 
// Fetch one value
$val = $executor->get_value('your_identifier', array( 'column' => $value ), 'column_name');

Parameters

Each Query consists of the following core parameters:

Title

Display name of the query.

Project

Associates the query with a specific project for organization.

Entity

Defines the returned data entity; important for joins and DataTable queries.

Database Table

The underlying database table, view, or DUAL in special cases.

Identifier

A unique key used for referencing the query in code or templates.

Cache (Bool)

Option to enable caching for the query.

DataTable Query (Bool)

Option to define whether the query should be used by a Wonderful Relations DataTable.

Subtable Search (Bool)

Option to enable search functionality within joined subtable records.

Description

Describes the purpose or scope of the query.

Limit

Restricts the number of returned rows. Relevant for non-DataTable queries.

Offset

Skips a number of rows in the result. Also relevant for non-DataTable queries.


Advanced Features & Configuration

Query Caching

If enabled, the query is compiled once and stored in the database. On subsequent requests, the cached version is used, optimizing performance without sacrificing flexibility.

Base Query

Ensures original table and field names are preserved—used when clean field names are required (e.g., for API usage).

Includes joined subqueries in search operations. Useful when searching across linked records.

Reusable Subqueries

Queries can include other queries, making it easy to compose complex logic with clean separation of concerns.

Joins

Visually add left/right joins between entities or tables.

Unions

Combine multiple result sets from separate queries into a single view.

Where & Filters

Add conditions, either static or dynamic via template filters (e.g., ##id##).

GroupBy & Having

Support for grouped aggregations and post-aggregation filtering.

OrderBy

Control default sorting of query results.

Aggregates

Used especially in DataTable footers or analytics to calculate sums, averages, etc.

Placeholder Filters

Example:

SELECT * FROM services WHERE project_id = ##project_id##

In the template, define:

"filter_project_id" => 42

The query will resolve dynamically to:

SELECT * FROM services WHERE project_id = 42

Conclusion

Queries in Wonderful Relations combine the power of SQL with the usability of a visual interface. They promote modularityreusability, and performance through caching and placeholder substitution.

By making queries configurable, not hardcoded, the system allows for:

  • Centralized query management
  • Easier debugging and auditing
  • Powerful nesting and composition
  • Dynamic integration across templates, DataTables, and PDF outputs

🚀 Queries are the foundation of data-driven logic in Wonderful Relations.