DQL查询的:运算符

时间:2013-12-25 03:54:22

标签: sql symfony dql

$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();

我对冒号以及它可以在查询中使用的不同位置有点困惑。 我认为:价格只是将价格声明为一个局部变量,然后在运行时将被19.99替换。

1)上述假设是否正确?

2)在DQL中是否有其他地方可以看到':',所以我不会将它与其他功能混淆。

2 个答案:

答案 0 :(得分:3)

1) Is the aforementioned assumption correct?

是的,:price实际上是parameter,用于避免像这样的丑陋代码:

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

相反,请使用参数表示法,然后调用setParamter()方法:

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

我不知道:)

答案 1 :(得分:2)

冒号:运算符用于预准备语句。它类似于PHP中变量命名的$字符,但专用于Doctrine。

您可能会注意到AcmeStoreBundle:Product内部也有冒号,但这是一个Symfony功能,您可以在其中定义实体所属的Bundle。在Doctrine中没有其他方法可以使用运算符。

相关问题