Question

$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT p
FROM AcmeStoreBundle:Product p
WHERE p.price > :price
ORDER BY p.price ASC'
)->setParameter('price', '19.99');

$products = $query->getResult();

I am a little bit confused about the colon and the different places it can be used in a query. I think :price is just declaring price as a local variable which will then be replace by 19.99 during runtime.

1) Is the aforementioned assumption correct?

2) Are there some other places you see the ':' in DQL so I don't confuse it with its other functions.

Was it helpful?

Solution 2

The colon : operator is used for prepared statements. It is like the $ character for variable naming in PHP, but dedicated to Doctrine.

You might notice the AcmeStoreBundle:Product has a colon inside of it too, but this is a Symfony feature where you can define the Bundle that the Entity belongs to. There's no other way to use the operator in Doctrine.

OTHER TIPS

1) Is the aforementioned assumption correct?

Yes, :price is in fact a parameter which is used to avoid ugly codes like this :

"SELECT ... WHERE p.price > " . $price

Instead, use the parameter notation, and after call the setParamter() method :

->setParameter('price', '19.99'); << Without prefix

More info from the doc

2) Are there some other places you see the ':' in DQL so I don't confuse it with its other functions.

I don't know for that :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top