mapping – 具有多个joinColumns的Doctrine 2 ManyToOne

前端之家收集整理的这篇文章主要介绍了mapping – 具有多个joinColumns的Doctrine 2 ManyToOne前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试根据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(),我会得到以下sql

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:

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;

    ...
}
?>

解决方法

原文链接:https://www.f2er.com/mssql/78779.html

猜你在找的MsSQL相关文章