2

我正在使用一个遗留数据库(基于 osCommerce 并且受到严重黑客攻击),其中包含一个名为 orders_total 的表,定义如下:

CREATE TABLE `orders_total` (
  `orders_total_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `orders_id` int(11) unsigned NOT NULL DEFAULT '0',
  `title` varchar(255) NOT NULL,
  `text` varchar(255) NOT NULL,
  `value` decimal(15,4) NOT NULL DEFAULT '0.0000',
  `class` varchar(40) NOT NULL,
  `sort_order` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`orders_total_id`),
  KEY `idx_order_id` (`orders_id`)
);

“类”列定义了该行所代表的“总计”类型。有点乱,有些“总数”根本不是总数,而是附加费,有些是折扣。一个例子:

+-----------------+-----------+--------------+----------+-----------+----------------------+------------+
| orders_total_id | orders_id | title        | text     | value     | class                | sort_order |
+-----------------+-----------+--------------+----------+-----------+----------------------+------------+
| 781797          | 190000    | Sub-Total:   | $1427.29 | 1427.2916 | ot_subtotal          | 1          |
| 781798          | 190000    | Courier:     | $172.05  | 172.0500  | ot_shipping          | 2          |
| 781799          | 190000    | Insurance:   | $47.62   | 47.6200   | ot_insurance         | 3          |
| 781800          | 190000    | Visa/MC Fee: | $41.80   | 41.8000   | ot_surcharge_visa_mc | 4          |
| 781801          | 190000    | <b>Total:</b>| $1688.76 | 1688.7616 | ot_total             | 10         |
+-----------------+-----------+--------------+----------+-----------+----------------------+------------+

我想做的是使用单表继承将表类拆分为 OrderTotal、OrderCharge 和 OrderDiscount 实体,然后通过与 Order 实体的一对多关系能够提取每个集合。我尝试了以下方法:

<?php

namespace Shop\Entity;

use Application\Entity\BaseEntity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="class", type="string")
 * @ORM\DiscriminatorMap({
 * "ot_cc_surcharge" = "OrderCharge",
 * "ot_coupon" = "OrderDiscount", 
 * "ot_insurance" = "OrderCharge",
 * "ot_payment_surcharge" = "OrderCharge",
 * "ot_paypal_surcharge" = "OrderCharge",
 * "ot_shipping" = "OrderCharge",
 * "ot_subtotal" = "OrderTotal", 
 * "ot_surcharge" = "OrderCharge", 
 * "ot_surcharge_amex" = "OrderCharge", 
 * "ot_surcharge_paypal" = "OrderCharge", 
 * "ot_surcharge_visa_mc" = "OrderCharge",
 * "ot_total" = "OrderTotal"
 * })
 * @ORM\Table(name="orders_total")
 * 
 */
class OrderTotalBase extends BaseEntity {
/*....*/
}

/**
 * @ORM\Entity
 * @ORM\Table(name="orders_total")
 */
class OrderCharge extends OrderTotalBase {
}

/**
 * @ORM\Entity
 * @ORM\Table(name="orders_total")
 */
class OrderTotal extends OrderTotalBase {
}

/**
 * @ORM\Entity
 * @ORM\Table(name="orders_total")
 */
class OrderDiscount extends OrderTotalBase {
}

/*....*/
class Order {
/*....*/
    /**
     * @ORM\OneToMany(targetEntity="OrderTotal", mappedBy="order", fetch="EAGER")
     * @var PersistentCollection
     */
    protected $orderTotals;

    /**
     * @ORM\OneToMany(targetEntity="OrderCharge", mappedBy="order", fetch="EAGER")
     * @var PersistentCollection
     */
    protected $orderCharges;

    /**
     * @ORM\OneToMany(targetEntity="OrderDiscount", mappedBy="order", fetch="EAGER")
     * @var PersistentCollection
     */
    protected $orderDiscounts;
}

然后将各自的 getter 和 setter 添加到 Order 类中。

问题是,当我调用 时$order->getOrderCharges(),它首先将整个记录列表作为OrderCharge对象返回。当我打电话时,$order->getOrderTotals()我得到完全相同的结果,但有OrderTotal对象。

STI 是否允许我仅根据 @DiscriminatorMap 获取正确的对象?

4

1 回答 1

3

虽然(如上所述)您的映射无效,但有一种方法可以根据 DQL 中的鉴别器列获取继承中的所有实体:

SELECT r FROM RootEntity r WHERE r INSTANCE OF SubType

或者只需使用 DQL 选择正确的类型:

SELECT e FROM SubType e

对于您的OrderTotal情况,该关联应该是

/**
 * @ORM\OneToMany(targetEntity="OrderTotal", mappedBy="totalOrder")
 * @var \Doctrine\Common\Collections\Collection
 */
protected $orderTotals;

一旦你修复了你的映射并且 CLI 验证器确认它(orm:validate-schema),该集合将被正确过滤

于 2013-02-26T09:14:04.593 回答