gpt4 book ai didi

php - 如何在 Symfony2 中连接 3 个表

转载 作者:行者123 更新时间:2023-11-30 00:43:43 25 4
gpt4 key购买 nike

我有 3 个表,我正在尝试将它们连接在一起。我可以加入“PurchaseOrderProductsStatus”表和“Product”表,但不能加入“PutAway”表,但我不确定如何加入其他表...

采购订单产品状态表

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

产品表

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

入库表

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

在我的产品实体中,我与 PutAway 表具有 OneToMany 关系。 PutAway 表和 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);

这是我的 Twig 模板:

{% 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 

最佳答案

我认为错误在于读取教义返回的对象。您正在执行混合查询。看看: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

不完全是这样,稍微玩一下。 twig 文件需要修改。

不过,如果数据库查询的数量合理,我不会通过自定义查询来完成它,而只是使用标准 Symfony,如果在 Controller 操作中执行以下操作,您编写的 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));

$entities 应该是你的 Twig 中的productActions。但在这种情况下,查询数量是合理的。

关于php - 如何在 Symfony2 中连接 3 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21564073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com