如何在 Symfony2 上自定义错误消息

发布于 2024-12-23 01:41:40 字数 520 浏览 5 评论 0原文

我正在尝试使用 DQL 在 ManyToMany 关系之间创建查询,这里是我的代码片段:

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery("SELECT * FROM TestGroupBundle:Question");

这是一个非常基本的 SQL 行,但我总是收到这个奇怪的错误:

[Syntax Error] line 0, col 7: Error: Expected IdentificationVariable | StateFieldPathExpression | AggregateExpression | "(" Subselect ")" | ScalarExpression, got '*'

500 Internal Server Error - QueryException

有人可以告诉我什么吗这意味着请以及如何解决它?谢谢

I'm trying to use DQL to create a query between a ManyToMany relation, here a snippet of my code:

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery("SELECT * FROM TestGroupBundle:Question");

It's a really basic SQL line, but I always get this weird error:

[Syntax Error] line 0, col 7: Error: Expected IdentificationVariable | StateFieldPathExpression | AggregateExpression | "(" Subselect ")" | ScalarExpression, got '*'

500 Internal Server Error - QueryException

Can someone tell me what does it mean please and how to fix it ? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘味果▽酱 2024-12-30 01:41:41

您混淆了 SQL 和 DQL。由于您正在使用对象模型,因此 DQL 中没有“*”。正确的语法是“SELECT q FROM TestGroupBundle:Question q”。结果包装在 \Doctrine\Common\Collections\ArrayCollection 对象中。您可以迭代该对象以获得结果。

关于 DQL,需要记住一件重要的事情:

初学者的一个常见错误是将 DQL 误认为只是某种形式的 SQL,因此尝试在查询中使用表名和列名或将任意表连接在一起。您需要将 DQL 视为对象模型的查询语言,而不是关系架构的查询语言。

You're mixing up SQL and DQL. There's no "*" in DQL since you're working with your object model. The proper syntax would be "SELECT q FROM TestGroupBundle:Question q". The result is wrapped in \Doctrine\Common\Collections\ArrayCollection object. You can iterate over the object to get your results.

There's one important thing to keep in mind about DQL:

A common mistake for beginners is to mistake DQL for being just some form of SQL and therefore trying to use table names and column names or join arbitrary tables together in a query. You need to think about DQL as a query language for your object model, not for your relational schema.

始终不够爱げ你 2024-12-30 01:41:41

Doctrine 并不总是正确解析命名空间快捷方式。尝试使用完整的命名空间而不是 TestGroupBundle:Question

Doctrine doesn't always parse namespace shortcuts correctly. Try using the full namespace instead of TestGroupBundle:Question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文