Symfony2 Doctrine自定义查询问题

时间:2014-02-14 16:55:08

标签: php mysql sql symfony doctrine-orm

我正在使用doctrine在Symfony2中创建自定义查询。 Anywho,我遇到的问题是这个脚本不会返回任何结果,除非所有表中的记录都有记录。即使所有连接的表都没有记录,我怎样才能确保我能够获得记录。

这是我的代码:

    $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表中存在记录。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

它应该使用LEFT JOIN代替。刚想通了,我想如果他们需要,我会把答案留给别人。干杯!