【发布时间】:2015-07-01 21:11:11
【问题描述】:
使用 symfony2/doctrine2,我很难为我的查询定义索引。
我的代码:
$queryBuilder = $this->_em
->createQueryBuilder()
->select('u, uis, cost, p, stock')
->from('AppBundle:FoodAnalytics\UserIngredient', 'u', 'p.id')
->leftJoin('u.product', 'p')
->leftJoin('u.numberObjects', 'stock')
->leftJoin('u.userIngredientSuppliers', 'uis')
->leftJoin('uis.numberObjects', 'cost')
->where('u.user = ?1')
->setParameter(1, $portfolioUser)
;
我收到以下错误:
[Semantical Error] line 0, col 110 near 'p LEFT JOIN u.numberObjects': Error: 'p' is already defined.
500 Internal Server Error - QueryException
1 linked Exception: QueryException »
[1/2] QueryException: SELECT u, uis, cost, p, stock FROM AppBundle:FoodAnalytics\UserIngredient u INDEX BY p.id LEFT JOIN u.product p LEFT JOIN u.numberObjects stock LEFT JOIN u.userIngredientSuppliers uis LEFT JOIN uis.numberObjects cost WHERE u.user = ?1 +
使用u.product 我得到以下错误:
[Semantical Error] line 0, col 87 near 'product LEFT': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
500 Internal Server Error - QueryException
1 linked Exception: QueryException »
仅使用 product,我收到以下错误:
[Semantical Error] line 0, col 85 near 'product LEFT': Error: 'product' does not point to a Class.
500 Internal Server Error - QueryException
1 linked Exception: QueryException »
如果我使用u.id,它可以正常工作
我只能按同一张表中的字段使用索引吗?
我该怎么做才能让它发挥作用?
非常感谢!
编辑:
作为临时修复我正在使用:
$result = $queryBuilder->getQuery()->getResult();
$result = array_combine(array_map(function(UserIngredient $userIngredient){
return $userIngredient->getProduct()->getId();
}, $result), $result);
return $result;
【问题讨论】:
-
我不确定您是否可以使用相关实体的字段来索引实体。
-
我可能完全错了,但您可以尝试使用
product.id而不是p.id进行索引。另外,这个:doctrine-project.org/jira/browse/DDC-1180 -
好的,product.id 不起作用。但是,您的参考表明已实施修复,但我尚未找到相应的解决方案。你有吗?
-
根据 PR,这可以工作:-
from('AppBundle:FoodAnalytics\UserIngredient', 'u', 'product')。是吗? -
指定索引的目标是从product表开始,然后加入UserIngredient表吗?如果 Product.Id 是主键,你可以从 Product 开始,不需要指定索引,然后与 UserIngredient 进行左连接。
标签: php mysql symfony doctrine-orm query-builder