Symfony2中的内连接查询

时间:2013-04-08 06:29:53

标签: php mysql symfony doctrine-orm

我的mysql查询:

SELECT p.name, c.id 
FROM customer c  
INNER JOIN sales_order so ON so.customer_id=c.id  
INNER JOIN sales_order_item soi ON so.id=soi.sales_order_id  
INNER JOIN product p ON p.id=soi.product_id  WHERE c.id=49454\G;

这是在MySql中工作,但是当这个查询在doctrine2中转换时,它会被给出错误

"[Semantical Error] line 0, col 87 near 'so ON so.customer_id=c.id': Error: Identification Variable sales_order used in join path expression but was not defined before."

以symfony形式我写这个:

$builder
        ->add('filter', 'entity', array(
            'label' => 'Show',
            'class' => 'RocketBraPrintBundle:SalesOrderItem',
            'query_builder' => function($er) {
                return $er->createQueryBuilder('p','so','c','soi')
                          ->select('p.name','c.id')
                          ->from('customer','c')
                          ->innerJoin('sales_order','so','ON','so.customer_id=c.id')
                          ->innerJoin('sales_order_item','soi','ON','so.id=soi.sales_order_id')
                          ->innerJoin('product','p', 'ON', 'p.id=soi.product_id')
                          ->where('c.id=49454\G');
            },
            'empty_value' => 'All',
            'required' => false
        ))

任何人都可以告诉我以symfony2方式转换这个mysql查询的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

->innerJoin('c.sales_order')

其中“sales_order”是“客户”实体的确切关系名称。

答案 1 :(得分:0)

这对我来说很复杂,因为我有点混淆,它就像这样转换。它正在工作。如果用户logedIn。

$er->createQueryBuilder('soi')
   ->select('soi','p','so')
   ->innerJoin('soi.salesOrder', 'so')
   ->innerJoin('soi.product', 'p')
   ->innerJoin('so.customer', 'c')
   ->groupBy('p.name')
   ->orderBy('p.name', 'ASC')
   ->where('so.customer = ' . $options['data']['customerid']);