Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3599496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:22:29+00:00 2026-05-18T20:22:29+00:00

I need a concrete sample of code with doctrine 2 that uses polymorphic associations.

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:22:30+00:00Added an answer on May 18, 2026 at 8:22 pm

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

    Refer to: Inheritance Mapping

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a concrete sample of code with doctrine 2 that uses polymorphic associations.
I have need to cast a generic list of a concrete type to a
I have a service that is hosted via WebServiceHost and I need to delegate
Note: All sample code is greatly simplified. I have a DLL defined as: using
I have a business layer class that uses System.IO.File to read information from various
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need to use own imaged markers instead built-in pins. I have several questions. 1.
need a quick help here. I have a series of hyperlinks all with the
Need some help to solve this. I have a gridview and inside the gridview
I have this code // was before var groupTransactions; IEnumerable<IGrouping<TransactionsGroupedByMonthYear, TransactionDto>> groupTransactions = null;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.