[For Developers] Introducing Database Flippers
- By captainerd
- 08/04/2025
- 0 comments
Introducing Database Flippers
What are Database Flippers?
Database Flippers are intended for advanced developers who are familiar with the event system and the framework, are there to future-proof VentoCart in terms of flexibility while developing extensions or plugins, just a design case that may come handy at one time.
Flippers allow developers to decouple logic from SQL queries.
This means you can intercept a query before it is executed and modify it based on specific needs. The concept of flippers in VentoCart is a powerful way to customize and manipulate database queries without modifying core files directly. You can apply these query modifications in a clean, decoupled way that keeps your code maintainable and extendable.
Flippers can be used to manipulate SQL queries (or any query-like data) before they are executed. This is particularly useful for situations where you need to intercept certain queries and alter their behavior based on dynamic factors, such as user roles, request parameters, or other business logic.
How to Register a Flipper
To register your own flipper in VentoCart, follow these steps:
-
Define Your Flipper Function: A flipper function is simply a callback that modifies the query. The function signature must accept one parameter (the SQL query) and return the modified query. Here's an example:
$flipper = function($sql) { // Modify the SQL query $sql .= " WHERE status = 'active'"; // Example modification return $this->db->query($sql); };
-
Register the Flipper: You can register your flipper using the
registerFlipper()
method. The tag you use here will identify the flipper for later use:$this->db->registerFlipper('active_products', $flipper);
-
Applying the Flipper: When executing a query, you can pass the tag of your flipper as an optional argument to the
query()
method. If a flipper is registered with the tag, it will be applied to the query before execution.Example of using the
active_products
flipper:$result = $this->db->query("SELECT * FROM products", 'active_products');
Event Listeners and Flippers
To ensure that flippers are applied to specific queries across the application, you can hook into events, such as:
-
Before Model Execution: Hook into a model’s "before" event to register flippers.
-
Top-Level Application Startup: Register flippers globally by hooking into
Catalog/controller/startup/application/before
.
For example:
// In the event handler or controller
$this->db->registerFlipper('product_query', $productFlipper);
Example: Pre-Defined Flippers in VentoCart
Currently, VentoCart includes a few predefined flippers, currently the model of catalog/product such as:
-
get_product
-
get_images
These are useful for altering product queries or image-related queries.
Q. Why must a flipper be defined in models and not use backtrace()?
A. Using backtrace()
is computationally expensive. It involves inspecting the call stack, which can add significant overhead to your application, especially when processing large volumes of requests. VentoCart aims to maintain high performance.
Q. Why not 'saveable' Flippers like events?
A. Flippers are meant to be used in conjuction with a registered event for the /before stage