Search code examples
sqlsymfonydql

For symfony2, when I see some codes, there are "something = :something"


What does "= :" mean in SQL or DQL?

Thanks!


Solution

  • That is a reference to parameter binding in DQL.

    http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#binding-parameters-to-your-query

    Note that numeric placeholders start with a ? followed by a number while the named placeholders start with a : followed by a string.

    you must then set your parameter with a ->setParameter() method.

    $qb->select('u')
       ->from('User', 'u')
       ->where('u.id = :identifier')
       ->orderBy('u.name', 'ASC')
       ->setParameter('identifier', 100); // Sets :identifier to 100, and thus we will fetch a user with u.id = 100
    

    This is good practice when using Doctrine because it is much more secure and prevent SQL Injection.