我正在使用原理在Symfony2中创建自定义查询。无论如何,我遇到的问题是,除非所有联接表中都有记录,否则此脚本将不会返回任何结果。即使所有联接的表都没有记录,我如何确保可以取回记录。

这是我的代码:

    $em = $this->getDoctrine()->getManager();
    $dql = "    SELECT p, poli, polil, pops, pa
                FROM WIC\PurchaseOrderBundle\Entity\PurchaseOrder p

                JOIN p.purchaseOrderLineItem poli
                  WITH poli.purchaseOrder=p.id

                JOIN poli.purchaseOrderLineItemLocation polil
                  WITH polil.purchaseOrderLineItem=poli.id

                JOIN polil.purchaseOrderProductsStatus pops
                  WITH pops.purchaseOrderLineItemLocation=polil.id

                JOIN pops.putAway pa
                  WITH pa.purchaseOrderProductsStatus=pops.id

                WHERE p.account=:account_id
                AND p.id=:id";
    $query = $em->createQuery($dql);
    $query->setParameters(array(
        'account_id' => $account->getId(),
        'id' => 10488,
    ));
    $purchaseorders = $query->getArrayResult();


计数数组:

 When I do a echo count(purchaseorders) it returns: 0


打印阵列:

 When I do a print_r($purchaseorders) it returns: Array ( )


我认为即使没有订单项,订单项位置,状态或收货记录,我也应该至少获取阵列中的采购订单信息。

这是我的测试树枝模板,错误不在这里,只是以为我还是要添加它...

{% for purchaseorder in purchaseorders %}
<div class="row-fluid">
    <div class="span12">
        Purchase Order: {{ purchaseorder.id }}
        <div class="row-fluid">
            <div class="span12">
                {% for purchaseorderitems in purchaseorder.purchaseOrderLineItem %}
                    &nbsp;&nbsp;&nbsp;&nbsp; {{ purchaseorderitems.id }}<br>
                    {% for purchaseorderitemLocations in purchaseorderitems.purchaseOrderLineItemLocation %}
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {{ purchaseorderitemLocations.id }}<br>
                        {% for purchaseOrderProductsStatus in purchaseorderitemLocations.purchaseOrderProductsStatus %}
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {{ purchaseOrderProductsStatus.id }}<br>
                        {% endfor %}
                    {% endfor %}
                {% endfor %}
            </div>
        </div>
    </div>
</div>
{% endfor %}


现在,我已经添加了新的采购订单,因此该记录确实存在于PurchaseOrder表中。

谢谢你的帮助!

最佳答案

它应该改为使用LEFT JOIN。只是弄清楚了,以为我会把答案留给其他任何需要的人。干杯!

10-04 13:33