Doctrine 2 具有多个 joinColumns 的 ManyToOne

发布于 2024-10-31 13:19:36 字数 2938 浏览 4 评论 0原文

我试图根据product_item 表中的productId 和toolboxItemId 选择product_item_sortorder 表中的匹配行。

在正常的 SQL 中,这将是针对给定的 ProductId:

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

如您所见,未找到引用的ColumnNames:

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;

    ...
}
?>

I'm trying to select the matching row in the product_item_sortorder table based on a productId and toolboxItemId from the product_item table.

In normal SQL that would be for a given productId:

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

I wrote the DQL for it as followed:

$this->_em->createQuery(
    'SELECT pi
    FROM Entities\ProductItem pi
    LEFT JOIN pi.sequence s
    WHERE pi.product = ?1'
);

Then I get following SQL if I output the $query->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

As you can see the referencedColumnNames are not found:

LEFT JOIN product_item_sortorder p1_ ON p0_.productId = p1_. AND p0_.toolboxItemId = p1_. 

Details of the product_item table:

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

Details of the product_item_sortorder table:

+-----+-----------+---------------+----------+
| id  | productId | toolboxItemId | sequence |
+-----+-----------+---------------+----------+
| 452 |         1 |             3 |        1 |
| 457 |         1 |             4 |        6 |
| 474 |         1 |            20 |        4 |
+-----+-----------+---------------+----------+

ProductItem Entity

<?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 Entity

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

    ...
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把梦留给海 2024-11-07 13:19:36

你的映射是严重错误的。您在两端都使用 ManyToOne,这怎么可能?您将两个关联定义为“拥有”端,没有映射者或反转者(请参阅“关联映射”章节)。并且您正在使用一个关联的连接列来映射到另一个实体中的许多字段。我想你想做点别的事情,你能准确描述你的用例吗?

Your mappings are seriously wrong. You are using ManyToOne on both ends, how is this possible? You have both associations defined as "owning"-side, no mapped-by or inversed-by (See Association Mappings chapter). And you are using join columns of one association to map to many fields in another entity. I suppose you want to do something else, can you describe exactly your use-case?

匿名。 2024-11-07 13:19:36

如何在 YAML 中映射示例(因为 @Hernan Rajchert 的示例仅在注释中):

ProductItem:
    type: entity

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

How you would map your example in YAML (since @Hernan Rajchert's example is only in annotations):

ProductItem:
    type: entity

    manyToOne:
        sequence:
            targetEntity: ProductItemSortorder
            joinColumns:
                productId:
                    referencedColumnName: productId
                toolboxItemId:
                    referencedColumnName: toolboxItemId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文