Orhan Saglam
Orhan Saglam

Reputation: 427

Polymorphic associations in doctrine 2?

I need a concrete sample of code with doctrine 2 that uses "polymorphic associations". Let me clarify myself. I have a Entity called Contract and a contract can have many price rules and these price rules can be different kind of classes and presisted in different tables. I suppose this is what's polymorphic associations for or am I wrong?

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
    }
}

There can be new types of price rules in the future,so how can I associate these rules to the main entity and presist them in seperate tables?

Update:

This is my new code:

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

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;

}

discountrule.php

namespace Entities;

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

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

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

}

extradiscountrule.php

namespace Entities;

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

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


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

}

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();

But when I try to add new rule to the contract I get error message (Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class Doctrine\Common\Collections\ArrayCollection is not a valid entity or mapped super class.)

What am I doing wrong ?

Upvotes: 4

Views: 2688

Answers (2)

Elliot Anderson
Elliot Anderson

Reputation: 346

You may be missing an @MappedSuperclass on your PriceRule parent object

Refer to: Inheritance Mapping

Upvotes: 5

Cobby
Cobby

Reputation: 5454

I don't this is possible because an interface cannot define properties in a class, so you can't guarantee there will be properties for Doctrine to manipulate.

If you could provide more details on your entities I could help better.

Upvotes: 0

Related Questions