-
PHQL (Phalcon Query Language) in the Phalcon framework is a high-level, object-oriented SQL dialect designed to interact with databases through Phalcon's ORM.
-
Relationship Handling:
Simplifies joins using predefined model relationships. For example, if User is related to Post, you can write:
SELECT u.name, p.title FROM User u JOIN Post p WHERE u.status = 'active'Without manually specifying ON clauses if relationships are defined in models.
important note: when tryping to bind params in sql query, note this: By default, bound parameters aren't casted in the PHP userland to the specified bind types, this option allows you to make Phalcon cast values before bind them with PDO. Link
-
In addition to standard transactions, Phalcon\Db provides built-in support for nested transactions (if the database system used supports them).
-
Profiling SQL Statements Phalcon\Db includes a profiling component called Phalcon\Db\Profiler, that is used to analyze the performance of database operations so as to diagnose performance problems and discover bottlenecks.
-
PHQL only allows data manipulation statements, avoiding altering or dropping tables/databases by mistake or externally without authorization. link
-
PHQL implements a high-level abstraction allowing you to handle tables as models and fields as class attributes.
-
These values that don't represent complete objects are what we call scalars.
<?php
$phql = "SELECT CONCAT(c.id, ' ', c.name) AS id_name FROM Cars AS c ORDER BY c.name";
$cars = $manager->executeQuery($phql);
foreach ($cars as $car) {
echo $car->id_name, "\n";
}- As we can query complete objects or scalars, we can also query both at once:
<?php
$phql = 'SELECT c.price*0.16 AS taxes, c.* FROM Cars AS c ORDER BY c.name';
$result = $manager->executeQuery($phql);The result in this case is an object Phalcon\Mvc\Model\Resultset\Complex.
- It's easy to request records from multiple models using PHQL. Most kinds of Joins are supported. As we defined relationships in the models, PHQL adds these conditions automatically:
<?php
$phql = 'SELECT Cars.name AS car_name, Brands.name AS brand_name FROM Cars JOIN Brands';
$rows = $manager->executeQuery($phql);
foreach ($rows as $row) {
echo $row->car_name, "\n";
echo $row->brand_name, "\n";
}By default, an INNER JOIN is assumed. You can specify the type of JOIN in the query:
<?php
$phql = 'SELECT Cars.*, Brands.* FROM Cars INNER JOIN Brands';
$rows = $manager->executeQuery($phql);
$phql = 'SELECT Cars.*, Brands.* FROM Cars LEFT JOIN Brands';
$rows = $manager->executeQuery($phql);
$phql = 'SELECT Cars.*, Brands.* FROM Cars LEFT OUTER JOIN Brands';
$rows = $manager->executeQuery($phql);
$phql = 'SELECT Cars.*, Brands.* FROM Cars CROSS JOIN Brands';
$rows = $manager->executeQuery($phql);- Phalcon doesn't only transform the PHQL statements into SQL. All events and business rules defined in the model are executed as if we created individual objects manually.
- what is the meaning of this line?
$category = di()->getDispatcher()->getParam('category');
-
di()- This function (or helper) is likely a shortcut to retrieve the Dependency Injector (DI) container in Phalcon.
- Phalcon uses a DI container to manage dependencies like services, configurations, and other objects.
-
di()->getDispatcher()- This calls the dispatcher service from the DI container.
- The dispatcher is responsible for handling the request and determining which controller and action to execute.
-
getParam('category')- Retrieves a parameter named
'category'from the request. - In Phalcon, route parameters are often passed via the URL or request and can be accessed through the dispatcher.
- Retrieves a parameter named
If your app has a route like:
https://example.com/products/laptops
where "laptops" is the category, and your route is defined as:
$router->add('/products/:params', [
'controller' => 'products',
'action' => 'index',
'category' => 1, // The first parameter is named 'category'
]);Then, inside your controller:
$category = di()->getDispatcher()->getParam('category');
echo $category; // Outputs "laptops"This line retrieves the "category" parameter from the request using the Phalcon dispatcher. The parameter likely comes from a route definition or a query string.