2013-02-26 126 views
2

我與舊的數據庫工作(自從基礎和大量黑客攻擊),其中包含一個名爲orders_total表,定義如下:學說2單表繼承的多對一的關係

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和setters添加到Order類中。

問題是,當我打電話給$order->getOrderCharges()時,它返回整個記錄列表,首先是OrderCharge對象。當我撥打$order->getOrderTotals()時,我得到的結果與OrderTotal對象完全相同。

STI允許我只根據@DiscriminatorMap獲取正確的對象嗎?

+0

您的映射無效:請驗證它們。所有這些關係你都不能擁有領域「秩序」。 – Ocramius 2013-02-26 09:03:23

回答

2

儘管(如上所述)的映射是無效的,有根據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),該集合將被正確過濾