Installation Add the package via Composer (though note: this is a PHP package for Phalcon, not Laravel—adaptation required):
composer require phalcon/phql
For Laravel, you’d need to integrate it via a bridge (e.g., phalcon/incubator or a custom wrapper).
First Use Case Write a basic PHQL query in a Laravel service:
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Phql\Parser;
// Initialize (adapt to Laravel's DB config)
$db = new DbAdapter(['host' => 'localhost', 'username' => 'root', 'password' => '', 'dbname' => 'test']);
$parser = new Parser($db);
// Execute a query
$result = $parser->parse("SELECT * FROM users WHERE id = :id:", ['id' => 1]);
Where to Look First
DB facade for integration hints (e.g., query builder patterns).phalcon/incubator for Laravel-Phalcon bridges (if available).Query Building Use PHQL for complex queries where Laravel’s query builder falls short (e.g., nested subqueries, advanced joins):
$phql = "SELECT u.*, COUNT(r.id) as review_count
FROM users u
LEFT JOIN reviews r ON u.id = r.user_id
WHERE u.status = :status
GROUP BY u.id";
$result = $parser->parse($phql, ['status' => 'active']);
Integration with Eloquent
class UserRepository {
public function findActiveWithReviews() {
$phql = "SELECT * FROM users WHERE status = 'active'";
return $this->parser->parse($phql)->toArray();
}
}
// In a service provider
Eloquent::macro('phql', function ($query) {
return $this->parser->parse($query);
});
Migrations Use PHQL for data seeding or complex migration logic:
$parser->parse("INSERT INTO users (name, email) VALUES (:name, :email)",
['name' => 'John', 'email' => '[email protected]']
);
$db = DB::connection()->getPdo();
$parser = new Parser($db);
Laravel-Phalcon Mismatch
Db\Adapter; Laravel’s DB facade uses PDO directly. Ensure compatibility:
// May throw errors; test thoroughly
$pdo = DB::connection()->getPdo();
$adapter = new DbAdapter(['pdo' => $pdo]);
phalcon/incubator or a custom adapter wrapper.Parameter Binding Quirks
PHQL uses :named placeholders, while Laravel uses ? or named bindings. Ensure consistency:
// PHQL expects named params
$parser->parse("SELECT * FROM users WHERE id = :id", ['id' => 1]);
No ORM Integration PHQL works with raw queries; avoid mixing with Eloquent’s active record unless manually bridged.
Performance Overhead PHQL parsing adds a layer of abstraction. Benchmark against raw PDO or Laravel’s query builder for critical paths.
$parser->setLogger(new \Phalcon\Logger\File('phalcon.log'));
->getRealSQL() to inspect generated queries:
$query = $parser->parse("SELECT * FROM users");
dd($query->getRealSQL());
Custom Functions
Extend PHQL with custom functions via Phalcon\Db\Functions\Specific:
class CustomFunctions extends \Phalcon\Db\Functions\Specific {
public function myCustomFunction($parser, $arguments) {
return "MY_CUSTOM_FUNC({$arguments})";
}
}
$parser->registerFunction('myCustomFunc', new CustomFunctions());
Query Builder Hooks
Intercept PHQL queries in Laravel’s DB facade by extending the query builder:
DB::extend('phalcon', function ($connection, $query) {
$parser = new Parser($connection->getPdo());
return $parser->parse($query->toSql(), $query->getBindings());
});
Event Listeners Use Phalcon’s events to modify queries before execution:
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach('db', function ($event, $connection) {
// Modify query here
});
$parser->setEventsManager($eventsManager);
How can I help you explore Laravel packages today?