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 6827677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:17:36+00:00 2026-05-26T22:17:36+00:00

I’ve a problem figuring out how to configure the mapping of my classes with

  • 0

I’ve a problem figuring out how to configure the mapping of my classes with Doctrine2.

Let say I’ve these tables:

Address table 
--------------------- 
- id 
- civic_no 
- road 
- state 
- country 

PersonnalAddress table 
--------------------- 
- id 
- base_address_id 
- type 
- is_primary 

BusinessAddress table 
--------------------- 
- id 
- base_address_id 
- business_name 
- shipping_phone 
- is_primary 

And thoses PHP objects:

class Address{}
class BusinessAddress extends Address{}
class PersonalAddress extends Address{}

Considering the following requirements:

  • An address can exist by itself (the Address class is not abstract)
  • A personalAddress and a businessAddress can have the very same address data
  • If I delete or edit the address, it has an impact on all the business or personal address that are inherited from it.
  • I don’t want any data duplication in the database (this is a requirement of the 2nd normal form)
  • Proxy methods mean code duplication, I prefer not to have any.
  • Magic methods are not good in term of testability, I prefer not to have any.

To better illustrate the problem, I expect the data in the data base to look like:

Address table:
id | civic_no | road | state | country
 1        123   test      qc        ca

PersonnalAddress table:
id | base_address_id | type | is_primary 
 1                 1      A            0
 2                 1      B            1

BusinessAddress table:
id | base_address_id | business_name | shipping_phone | is_primary 
 1                 1       chic choc          1231234            1

What would be the best strategy to implement a solution that match theses requirements ?

  • 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-26T22:17:37+00:00Added an answer on May 26, 2026 at 10:17 pm

    Ok this is a bit of a long one but I think it covers all your bases, if you have any questions then feel free to ask.

    This comes with a caveat that I don’t know if you can do Many-To-One on a MappedSuperclass. If that isn’t possible then you may be able to use Class Table Inheritance instead. Give it a try and tell us if it works.

    Keep in mind I pushed this code out pretty quickly, it is untested so it may not be correct but hopefully you’ll get the idea of how it works.

    Here we go!

    Interface to make it easier to say “what is an address” without making abstract classes then overriding methods and causing the “bad design” feeling 😉

    interface Address {
    
        function getCivicNo();
    
        function getRoad();
    
        function getState();
    
        function getCountry();
    }
    

    Abstract Entity which you don’t really need but if you dont use it you need to duplicate the ID code.

    /**
     * @ORM\MappedSuperclass
     */
    abstract class AbstractEntity {
    
        /**
         * Entity ID column.
         * 
         * @var integer
         * 
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue
         */
        private $id;
    
        public function getId() {
            return $id;
        }
    
    }
    

    BasicAddress which you can store alone or have linked to a “ComplexAddress”

    /**
     * @ORM\Entity
     */
    class BasicAddress extends AbstractEntity implements Address {
    
        /** @ORM\Column() */
        private $road;
    
        public function getRoad() {
            return $this->road;
        }
    
        // etc etc
    
    }
    

    “ComplexAddress” is just here to let you re-use the code for delegating calls to a basic address.

    /**
     * @ORM\MappedSuperclass
     */
    abstract class ComplexAddress extends AbstractEntity implements Address {
    
        /** @ORM\Many-To-One(targetEntity="BasicAddress")
        private $basicAddress;
    
        public function __construct(BasicAddress $basicAddress) {
            $this->basicAddress = $basicAddress;
        }
    
        public function getRoad() {
            return $this->basicAddress->getRoad();
        }
    
        // other methods for implementing "Address" just delegate to BasicAddress
    
    }
    

    PublicAddress

    /**
     * @ORM\Entity()
     */
    class PersonalAddress extends ComplexAddress {
    
        /** @ORM\Column(type="boolean") */
        private $isPrimary;
    
        public function isPrimary() {
            return $isPrimary;
        }
    
        // other personal address methods here
    
    }
    

    BusinessAddress

    /**
     * @ORM\Entity()
     */
    class BusinessAddress extends ComplexAddress {
    
        /** @ORM\Column() */
        private $businessName;
    
        public function getBusinessName() {
            return $this->businessName;
        }
    
        // other business address methods here
    
    }
    

    Edit: Just noticed I forgot to put cascade parameters for deletion, you might need to handle this directly though – when a BasicAddress is deleted also delete the other addresses that use it.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.