John White
John White

Reputation: 19

Doctrine2 LEFT JOIN condition1 OR condition2

this thing really impressed me, that i can't do such a thing as join with ON clause where i have ( condition 1 OR condition 2 ) .I found constant ON but that throwing exception "Expected end of string, got 'ON'" and when i use with doctrine do automatic join as expected but after that i want OR and not AND between conditions. Is this even possible to do ? I think this is nothing special but in doctrine it looks like ...

$qb = $this->entity_manager->createQueryBuilder();
$result = $qb->select(array('s','pb','p','pa','a','ag','st'))
             ->from('StockContent','s','s.id_product_box')
             ->join('s.product_box','pb','WITH','s.product_box = pb OR s.product_box = pb.universal_package')
             ->join('pb.product','p')
             ->join('s.stock', 'st')
             ->leftJoin('pb.productattribute','pa')
             ->leftJoin('pa.attribute','a')
             ->leftJoin('a.attributegroup','ag')
             ->where('p.id_product = :id_product')
             ->setParameter('id_product', (int)$id_product);
if((int)$stock > 0){
    $result->andWhere('s.stock = :stock')
           ->setParameter('stock',(int)$stock);
}
$result->orderBy('pb.box_number');
return $result->getQuery()->getResult();

So the universal package is reference to same table other row and i need to pick content from stockcontent and join rows where id_box or universal_package are the one from stockcontent

Upvotes: 1

Views: 82

Answers (1)

Javier C. H.
Javier C. H.

Reputation: 2123

How about this:

->join(
    's.product_box',
    'pb',
    Expr\Join::WITH,
    $qb->expr()->orX($qb->expr()->eq('s.product_box', 'pb'), 's.product_box = pb.universal_package')
)

Upvotes: 1

Related Questions