学说2中的多态联想?

Orh*_*han 4 doctrine domain-driven-design polymorphic-associations doctrine-orm

我需要一个具有学说2的代码的具体示例,它使用"多态关联".让我澄清一下自己.我有一个名为Contract的实体,合约可以有很多价格规则,这些价格规则可以是不同类别的类,并且存在于不同的表中.我想这是多态关联,或者我错了?

class contract {

    private $id;

    private $priceRules;


}

class discountRule implements priceRule{

    function calculate() {
         // calculate new price after this rule
    }
}

class extraSpecialRule implements priceRule {

    function calculate() {
        // calculate new price after this rule
    }
}
Run Code Online (Sandbox Code Playgroud)

将来可能会有新类型的价格规则,那么我如何将这些规则与主要实体相关联并将它们固定在单独的表中呢?

更新:

这是我的新代码:

contract.php

namespace Entities;

use Doctrine\Common\Collections\ArrayCollection;


/**
 * @Entity @Table(name="contract") 
 */ 
class Contract {

    /**
     * 
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * 
     * @Column(type="integer")
     */
    private $propertyId;

    /**
     * 
     * @Column(type="integer")
     */
    private $agencyId;

    /**
     * 
     * @OneToMany(targetEntity="priceRule" ,mappedBy="contract")
     * 
     */
    private $priceRules;

    public function __construct($propertyId,$agencyId){
        $this->propertyId=$propertyId;
        $this->agencyId=$agencyId;
        $this->priceRules=new ArrayCollection();
    }

    public function addPriceRule(priceRule $rule){
        $this->priceRules[]=$rule;  
    }

    public function getPriceRules(){
        return $this->priceRules;
    }
}
Run Code Online (Sandbox Code Playgroud)

pricerule.php

namespace Entities;

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr" , type="string")
 * @DiscriminatorMap({"discountrule"="discountRule","extradiscountrule"="extraDiscountRule"})
 */
class priceRule{
    /**
     * 
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

     /**
      * 
      * @ManyToOne(targetEntity="contract",inversedBy="availibilityRules")
      * @JoinColumn("contract_id",referencedColumnName="id")
      */
    private $contract;

}
Run Code Online (Sandbox Code Playgroud)

discountrule.php

namespace Entities;

/**
 * @Entity
 * 
 *
 */
class discountRule extends priceRule {

    /**
     * 
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    public function calculatePrice(){
         // calculate new price
    }

}
Run Code Online (Sandbox Code Playgroud)

extradiscountrule.php

namespace Entities;

/**
 * @Entity
 * 
 *
 */
class extraDiscountRule extends priceRule {

    /**
     * 
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;


        public function calculate() {
            // calculate new price
        }

}
Run Code Online (Sandbox Code Playgroud)

sampleusage.php

$contract=new Contract(1,1);
$discount=new discountRule();

$em->persist($discount);

$contract->addPriceRule($discount);

$em->persist($contract->getPriceRules());
$em->persist($contract);

$em->flush();
Run Code Online (Sandbox Code Playgroud)

但是当我尝试向合同添加新规则时,我收到错误消息(致命错误:未捕获异常'Doctrine\ORM\Mapping\MappingException',消息'Class Doctrine\Common\Collections\ArrayCollection不是有效实体或映射超类.)

我究竟做错了什么 ?

小智 5

您可能在PriceRule父对象上缺少@MappedSuperclass

请参阅:继承映射