查询生成器/DQL 不适用于 INNER JOIN - 语法问题
我知道我这里有语法问题,但我无法弄清楚。我正在尝试对 5 个表进行 SELECT 和 INNER JOIN,但 Symfony 抱怨 JOIN 中的实体在定义之前就被使用了。
实际错误如下:[语义错误]第0行,第121列靠近'I ON C.id =':错误:识别变量MySiteBundle:在连接路径表达式中使用的项目但之前未定义。
这是 PHP 代码。
注意:我已将此查询缩短为两列、两个表和一个联接,以保持问题简单并表明我的观点。实际的查询要长得多并且会产生相同的错误。
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'select C.name as CName, I.id as IId
FROM MySiteBundle:Categories C
INNER JOIN MySiteBundle:Items I ON C.id = I.category_id');
$result = $query->getResult();
更新
正如建议的那样,我已经放弃了 DQL 代码并使用查询生成器代码。我收到一个非常类似的错误,内容为 'Categories c': Error: Class 'Categories' is not Define
。我的 QB 代码如下。
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder()
->select('c.name, i.id, i.image, i.name, i.description, m.id, m.quantity, m.value, m.qty_received, m.custom_image, m.custom_name, m.custom_description, u.user1fname, u.user1lname, u.user2fname, u.user2lname')
->from('Categories', 'c')
->innerJoin('Items', 'i', 'ON', 'c.id = i.category_id')
->innerJoin('MemberItems', 'm', 'ON', 'i.id = m.item_id')
->innerJoin('User', 'u', 'ON', 'm.memberinfo_id = u.id')
->where('u.id = ?', $slug)
->orderBy('c.id', 'ASC')
->getQuery();
$memberItems = $qb->getResult();
有什么建议吗?
I know I have a syntax isse here however I cant figure it out. I'm trying to do a SELECT and INNER JOIN of 5 tables but Symfony is complaining about the Entities in the JOIN are used before being defined.
Actual error is as follows: [Semantical Error] line 0, col 121 near 'I ON C.id = ': Error: Identification Variable MySiteBundle:Items used in join path expression but was not defined before.
Here is the PHP code.
Note: I have shortened this query to two columns, two tables, and one join to keep the question simple and show my point. The actual query is much longer and is producing the same error.
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'select C.name as CName, I.id as IId
FROM MySiteBundle:Categories C
INNER JOIN MySiteBundle:Items I ON C.id = I.category_id');
$result = $query->getResult();
Update
As suggested I've done away with the DQL code and am using Query Builder code. I'm getting a very similiar error which says 'Categories c': Error: Class 'Categories' is not defined
. My QB code is below.
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder()
->select('c.name, i.id, i.image, i.name, i.description, m.id, m.quantity, m.value, m.qty_received, m.custom_image, m.custom_name, m.custom_description, u.user1fname, u.user1lname, u.user2fname, u.user2lname')
->from('Categories', 'c')
->innerJoin('Items', 'i', 'ON', 'c.id = i.category_id')
->innerJoin('MemberItems', 'm', 'ON', 'i.id = m.item_id')
->innerJoin('User', 'u', 'ON', 'm.memberinfo_id = u.id')
->where('u.id = ?', $slug)
->orderBy('c.id', 'ASC')
->getQuery();
$memberItems = $qb->getResult();
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
路易斯在我打字的时候发了帖子。那好吧。
DQL 根据您的关联为您处理加入详细信息。一般来说,你只需要拼出FROM类名即可。类似于:
并明确使用查询生成器。
=================================================== ===========================
这里是在 Symfony 2 中使用查询生成器的示例。
Louis posted while I was typing. Oh well.
DQL takes care of the join details for you based on your associations. In general, you only need to spell out the FROM class name. Something like:
And definitively use query builder.
=============================================================================
Here is an example of using query builder in Symfony 2.
DQL 不使用这样的联接。它们有点简化。然而我也发现它们的记录不足。
实际使用的关系取决于您的模型。
我通常使用查询生成器。
DQL does not use joins like that. They are a bit simplified. However I also found them to be underdocumented.
The actual relation used depends on your model.
I normally use the query builder.
您可能需要检查注释或 yml 文件,以确保 OneToMany、ManyToMany 和 ManyToOne 的映射设置正确。
在您的 MemberItems 控制器或 MemberItems 存储库中放置以下内容:
You may have to check your annotations or yml file to make sure you have your mapping setup correctly for OneToMany, ManyToMany, and ManyToOne.
In your MemberItems Controller or MemberItems Repository put this: