4845 explained code solutions for 96 technologies

php-symfonyHow to use the Query Builder in PHP Symfony?

The Query Builder in PHP Symfony is a powerful tool for creating and executing database queries. It allows you to easily create complex SQL queries without having to write raw SQL.


$qb = $this->createQueryBuilder('u');
   ->where('u.age > :age')
   ->setParameter('age', 18);

This example will generate the following SQL query:

SELECT u.name
FROM users u
WHERE u.age > 18

Code explanation

  • $qb = $this->createQueryBuilder('u'); - creates a new query builder object
  • $qb->select('u.name') - selects the name column from the users table
  • ->where('u.age > :age') - adds a WHERE clause to the query
  • ->setParameter('age', 18) - sets the age parameter to 18

Helpful links

Edit this code on GitHub