문제

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

도움이 되었습니까?

해결책 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.

다른 팁

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 :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top