2011-04-08 73 views
7

我想從product_item表中選擇基於productId和toolboxItemId的product_item_sortorder表中的匹配行。Doctrine 2 ManyToOne with multiple joinColumns

在正常的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' 
); 

然後我得到下面的SQL如果我輸出的$查詢 - > getSQL():

SELECT 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 

正如你可以看到referencedColumnNames都沒有發現:

012在product_item表個
LEFT JOIN product_item_sortorder p1_ ON p0_.productId = p1_. AND p0_.toolboxItemId = p1_. 

細節:product_item_sortorder表

+-----+-----------+---------------+ 
| id | productId | toolboxItemId | 
+-----+-----------+---------------+ 
| 467 |   1 |    3 | 
| 468 |   1 |   10 | 
| 469 |   1 |   20 | 
| 470 |   1 |    4 | 
| 471 |   1 |   10 | 
+-----+-----------+---------------+ 

詳情:

+-----+-----------+---------------+----------+ 
| 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; 

    ... 
} 
?> 

回答

3

您的映射嚴重錯誤。你在兩端使用ManyToOne,這怎麼可能?您將兩個關聯定義爲「擁有」,不映射或反轉(請參閱關聯映射章節)。並且您正在使用一個關聯的連接列來映射另一個實體中的許多字段。我想你想做別的事情,你能描述一下你的用例嗎?

+0

我更新了一點問題位和下面你可以看到我想執行哪個查詢 – tom 2011-04-12 10:15:40

1

你會如何映射在YAML你的榜樣(因爲@Hernan Rajchert的例子只是說明):

ProductItem: 
    type: entity 

    manyToOne: 
     sequence: 
      targetEntity: ProductItemSortorder 
      joinColumns: 
       productId: 
        referencedColumnName: productId 
       toolboxItemId: 
        referencedColumnName: toolboxItemId