简体   繁体   中英

innerJoin Query in Symfony2

I mysql query :

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;

this is working in MySql but when this query convert in doctrine2 then it's given error

"[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."

In symfony form I write this:

$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
        ))

Can any one tell me what is right way to convert this mysql query in symfony2 way ?

->innerJoin('c.sales_order')

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

it is complicated for me because i have bit confuse and It's convert like this.It's working.if user 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']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM