简体   繁体   English

Symfony2学说,在条件下显示数据库中的实体

[英]Symfony2 Doctrine, displaying entities from db under conditions

I have 3 tables that stores information of packages that comes from the warehouse. 我有3个表,用于存储来自仓库的包装信息。

Table 1 (main): 表1(主):

id | pkgno | barcode | weight

Table 2 (sub): 表2(子):

id | mainid | boxno | rackno | batchno | inqty 

Table 3 (shipment): 表3(出货量):

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)? 我如何查询仅在inqty减去outqty不等于0(所以1或更多)的情况下显示的数据行?

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? 我该如何设置它,以便当类似$ currentqty = $ inqty-$ outqty时,if($ currentqty> 0)然后显示数据? I know how to do it in simple php, but not quite sure about Symfony2. 我知道如何用简单的php来做,但是对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. 编辑:更多说明:因此,并非所有的Sub表实体都具有Shipment条目,也就是说,如果该包裹尚未被运送,则为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. 我想满足以下条件:当前数量将空数据(空)设为0。因此,如果Sub没有Shipment条目,则inqty将减去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. 我通过将null值设置为0通过coalesce()函数找到了解决方案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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