我正在尝试根据product_item表中的productId和toolboxItemId选择product_item_sortorder表中的匹配行。
在给定productId的正常SQL中:
SELECT pi.*, pis.* FROM product_item pi
LEFT JOIN product_item_sortorder pis
ON pi.productId = pis.productId
AND pi.toolboxItemId = pis.toolboxItemId
WHERE pi.productId = 6
我为它编写了DQL,如下所示:
$this->_em->createQuery(
'SELECT pi
FROM Entities\ProductItem pi
LEFT JOIN pi.sequence s
WHERE pi.product = ?1'
);
如果我输出$ query-> getSQL():
,那么我会得到以下SQLSELECT p0_.id AS id0, p0_.productId AS productId1, p0_.priceGroupId AS priceGroupId2, p0_.toolboxItemId AS toolboxItemId3, p0_.levelId AS levelId4, p0_.parentId AS parentId5, p0_.productId AS productId6, p0_.toolboxItemId AS toolboxItemId7 FROM product_item p0_ LEFT JOIN product_item_sortorder p1_ ON p0_.productId = p1_. AND p0_.toolboxItemId = p1_. WHERE p0_.productId = ? ORDER BY p0_.id ASC
如您所见,找不到referencesColumnNames:
LEFT JOIN product_item_sortorder p1_ ON p0_.productId = p1_. AND p0_.toolboxItemId = p1_.
product_item表的详细信息:
+-----+-----------+---------------+
| id | productId | toolboxItemId |
+-----+-----------+---------------+
| 467 | 1 | 3 |
| 468 | 1 | 10 |
| 469 | 1 | 20 |
| 470 | 1 | 4 |
| 471 | 1 | 10 |
+-----+-----------+---------------+
product_item_sortorder表的详细信息:
+-----+-----------+---------------+----------+
| id | productId | toolboxItemId | sequence |
+-----+-----------+---------------+----------+
| 452 | 1 | 3 | 1 |
| 457 | 1 | 4 | 6 |
| 474 | 1 | 20 | 4 |
+-----+-----------+---------------+----------+
ProductItem实体
<?php
/**
* @Entity(repositoryClass="Repositories\ProductItem")
* @Table(name="product_item")
*/
class ProductItem
{
...
/**
* @ManyToOne(targetEntity="ProductItemSortorder")
* @JoinColumns({
* @JoinColumn(name="productId", referencedColumnName="productId"),
* @JoinColumn(name="toolboxItemId", referencedColumnName="toolboxItemId")
* })
*/
protected $sequence;
...
?>
ProductItemSortOrder实体
<?php
/**
* @Entity(repositoryClass="Repositories\ProductItemSortorder")
* @Table(name="product_item_sortorder")
*/
class ProductItemSortorder
{
...
/**
* @ManyToOne(targetEntity="Product")
* @JoinColumn(name="productId", referencedColumnName="id")
*/
protected $product;
/**
* @ManyToOne(targetEntity="ToolboxItem")
* @JoinColumn(name="toolboxItemId", referencedColumnName="id")
*/
protected $toolboxItem;
...
}
?>
答案 0 :(得分:8)
检查此示例中的复合连接列 https://github.com/doctrine/doctrine2/blob/2.0.6/tests/Doctrine/Tests/Models/Navigation/NavPhotos.php
答案 1 :(得分:3)
你的映射严重错误。你在两端使用ManyToOne,这怎么可能?您将这两个关联定义为“拥有” - 侧,没有映射或反向(请参阅关联映射章节)。并且您正在使用一个关联的连接列来映射到另一个实体中的许多字段。我想你想做别的事情,你能描述一下你的用例吗?
答案 2 :(得分:1)
如何在YAML中映射您的示例(因为@Hernan Rajchert的示例仅在注释中):
ProductItem:
type: entity
manyToOne:
sequence:
targetEntity: ProductItemSortorder
joinColumns:
productId:
referencedColumnName: productId
toolboxItemId:
referencedColumnName: toolboxItemId