如何在Symfony2中加入3个表

时间:2014-02-04 21:40:47

标签: php mysql symfony join doctrine-orm

我有3张桌子,我试图加入。我可以加入“PurchaseOrderProductsStatus”表和“Product”表,但不能加入“PutAway”表,但我不确定如何加入其他表...

PurchaseOrderProductsStatus表

id  | product_id | 
------------------
10  | 1          |

产品表

id  | Name      | 
-----------------
1   | Acme      |

PutAway表

id  | product_id | 
-----------------
100 | 1

在我的产品实体中,我与PutAway表有一个OneToMany关系。 PutAway表和Product表由Product Id连接。

/**
 * @ORM\OneToMany(targetEntity="WIC\InventoryBundle\Entity\PutAway", mappedBy="product", fetch="EAGER")
 */
protected $putAway;

public function __construct()
{
    $this->putAway = new ArrayCollection();
}

在我的PurchaseOrderProductsStatus自定义查询中,我能够加入产品表,我只是无法检索PutAway结果的集合。我该怎么做?

这是我的问题:

$query = $this->getEntityManager()
->createQuery('
SELECT      p, pr
FROM        WIC\PurchaseOrderBundle\Entity\PurchaseOrderProductsStatus p
JOIN        p.product pr
WHERE       p.inventoryLocation = :id
AND         p.account = :account_id
GROUP By    p.product
')
->setParameter('id', $id)
->setParameter('account_id', $account_id);

这是我的树枝模板:

{% for action in productActions %}
    <tr>
        <td>{{ action.product.id }}</td>
        <td>{{ action.product.sku }}</td>
        <td>{{ action.product.name }}</td>
        <td>{{ action.qty }}</td>
        <td>0</td>
    </tr>
    <tr>
        <td colspan="5">
            <div class="row-fluid">
                <div class="span2">
                    <table class="table table-striped table-bordered">
                        <thead>
                        <tr>
                            <th>Purchase Order</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td></td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <div class="span10">
                    <table class="table table-bordered" id="put_away_{{ action.product.id }}">
                        <thead>
                        <tr>
                            <th>Put Away Location</th>
                            <th>Quantity</th>
                            <th>Date</th>
                            <th>Entered By</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for putAway in action.product.putAway %}
                            <tr class="info">
                                <td>{{ putAway.inventoryLocation.name }}</td>
                                <td>{{ putAway.qty }}</td>
                                <td>{{ putAway.created|date("m/d/Y") }}</td>
                                <td>{{ putAway.createdBy.firstName }} {{ putAway.createdBy.lastName }}</td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </td>
    </tr>
{% endfor %}

我收到以下错误:

Key "putAway" for array with keys "id, archived_id_number, name, alternative_name, case_qty, description, sku, upc, unit_cost, retail_price, map_price, productWeightBig, productWeightSmall, productWeightUnit, productLength, productWidth, productHeight, productDimensionUnit, shippingWeightBig, shippingWeightSmall, shippingWeightUnitBig, shippingWeightUnitSmall, shippingLength, shippingWidth, shippingHeight, shippingDimensionUnit, path, created, updated, deletedAt" does not exist in 

1 个答案:

答案 0 :(得分:0)

我认为错误是关于阅读教义返回的对象。你正在做一个混合查询。看看:http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html

无论如何,我会做的完全不同,您应该考虑从哪个对象执行查询:PurchaseOrderProductsStatus有一个Product,即可,但此连接有多个“PutAway”。我首先要查询Put Away对象:

SELECT      pa, pr, pops
FROM        WIC\PurchaseOrderBundle\Entity\PutAway pa
LEFT JOIN        pa.product pr
LEFT JOIN        pr.PurchaseOrderProductsStatus pops
WHERE       p.inventoryLocation = :id AND p.account = :account_id

不完全是这样,玩一点。树枝文件需要修改。

尽管如此,如果数据库查询的数量是合理的,我不会通过自定义查询来完成,但只是使用标准的Symfony,如果在你的控制器操作中,你编写的TWIG应该正常工作:

$id= the id that you used on your query
$account= the object whose id you used on your query
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('WICPurchaseOrderBundle:PurchaseOrderProductsStatus')
->findAby(array('id'=>$id, 'account'=>$account));

$实体应该是你枝条中的productActions。但只是在查询数量合理的情况下。