Skip to content

Instantly share code, notes, and snippets.

@zahra-ove
Last active March 10, 2025 06:38
Show Gist options
  • Select an option

  • Save zahra-ove/66353b22a8f2f1ec27bc9d8e49c56629 to your computer and use it in GitHub Desktop.

Select an option

Save zahra-ove/66353b22a8f2f1ec27bc9d8e49c56629 to your computer and use it in GitHub Desktop.

PHQL

  1. 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.

  2. 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

  1. In addition to standard transactions, Phalcon\Db provides built-in support for nested transactions (if the database system used supports them).

  2. 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.

  3. PHQL only allows data manipulation statements, avoiding altering or dropping tables/databases by mistake or externally without authorization. link

  4. PHQL implements a high-level abstraction allowing you to handle tables as models and fields as class attributes.

  5. 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";
}
  1. 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.

  1. 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);
  1. 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.

  1. what is the meaning of this line?
$category = di()->getDispatcher()->getParam('category');

Breaking it Down:

  1. 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.
  2. 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.
  3. 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.

Example of How It Works in a URL:

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"

Summary:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment