我对 QueryBuilder 有问题,我有三个表:
“计数器” “cost_tp” “counter_cost_tp”(counter 和 cost_tp 之间的多对多关系)
在原始 SQL 中我可以创建查询:
SELECT `counter`.`name`, `cost_tp`.`name` FROM `counter`
RIGHT JOIN `counter_cost_tp` ON `counter`.`id` = `counter_cost_tp`.`counter_id`
JOIN `cost_tp` ON `cost_tp`.`id` = `counter_cost_tp`.`cost_tp_id`
我试图在 Symfony 的 QueryBuilder 中做同样的事情:
$builder
->select(['c', 'ct'])
->from(CostTp::class, 'ct')
->innerJoin('ct.counters', 'c')
->distinct();
但效果不佳,仅返回 CostTp 和第一个计数器的结果。当我尝试运行 getQuery()->getSQL() 时,原始 sql 工作正常,但 getQuery->getResult() 返回错误结果。
你有什么好的方法来正确加入QueryBuilder吗?