简体   繁体   中英

Symfony2 Doctrine, displaying entities from db under conditions

I have 3 tables that stores information of packages that comes from the warehouse.

Table 1 (main):

id | pkgno | barcode | weight

Table 2 (sub):

id | mainid | boxno | rackno | batchno | inqty 

Table 3 (shipment):

id | subid | shipdate | blno | outqty

How would I query for rows of data that only displays under the condition that when inqty subtracts outqty does not equal to 0 (so 1 or more)?

This is the query that I started with:

SELECT
   s.id as subid, s.rackno, s.boxno, s.batchno, s.inqty,
   m.pkgno, m.barcode, m.weight
FROM
   Bundle:Main m
LEFT JOIN
   m.sub s
WHERE
   m.pkgno = :pkgno

So this would give me all the ones with the package number regardless of the quantities. How can I set it so that when something like $currentqty = $inqty - $outqty, if($currentqty > 0 ) then display data? I know how to do it in simple php, but not quite sure about Symfony2.

EDIT: more explanation: So not all of the Sub table entities have a Shipment entry, that is if that package has not been shipped yet then it is null. I'd like to have the condition that the current quantity would take the empty data (null) as a 0. Therefore, if Sub does not have Shipment entry, then inqty would subtract 0.

Copied from the doctrine docs

$query = $em->createQuery('SELECT u FROM CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000');

So you can do arithmetic using columns by wrapping each arithmetic operation in parenthesis. So just join the three tables and subtract your two values.

WHERE
    table2.outqty IS NOT NULL AND
    (table2.inqty - table3.outqty) > 0

I found the solution via the coalesce() function by setting null values to 0.

SELECT
  s.id as subid, s.rackno, s.boxno, s.batchno, s.inqty,
  m.pkgno, m.barcode, m.weight,
  ss.outqty
FROM
  Bundle:Main m
LEFT JOIN
  m.sub s
LEFT JOIN
  s.ship ss
WHERE
  m.pkgno = :pkgno
AND
  (s.inqty - coalesce(ss.outqty, 0)) > 0

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