我正在使用Legacy数据库(基于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`)
);
“class”列定义行所代表的“total”类型。有点乱,因为有些“总数”根本不是总数,而是附加费,有些是折扣。一个例子:
+-----------------+-----------+--------------+----------+-----------+----------------------+------------+
| 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获取正确的对象?
答案 0 :(得分:3)
虽然(如上所述)您的映射无效,但有一种方法可以根据DQL中的discriminator列获取继承中的所有实体:
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
)后,该集合将被正确过滤