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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:10:25+00:00 2026-05-24T19:10:25+00:00

I have three entities: User, Office and PhoneNumber. The user has many phone numbers,

  • 0

I have three entities: User, Office and PhoneNumber. The user has many phone numbers, and the office has many phone numbers too.

The problem is how to represent these entities relations in Doctrine 2.

  1. At first I tried to use bi-directional one-to-many associations
    (User -> has many -> PhoneNumbers) (Office -> has many ->
    PhoneNumbers), the PhoneNumber has two mapping fields, one for User
    and anotherone for Office. This solution doesn’t work since one of
    the mapping foreign keys couldn’t be null.

  2. My second approach was to use two entities and one superclass for PhoneNumber. The PhoneNumber superclass has defined all common fields except the mapping field. Entities UserPhoneNumber and
    OfficePhoneNumber extended the PhoneNumber entity and specified the
    different mapping field and different table. (one table for OfficePhoneNumbers, anotherone for UserPhoneNumbers)

    This solution actually works, but it is quite ugly to have 3
    classes to represent one simple entity.

  3. My third approach is to use uni-directional one-to-many mapping. This will eliminate the need of mapping field for the PhoneNumber entity. The problem is that when I use cascade remove for the many-to-many field, it violates the integrity constraint when deleting records.

    When I omit the cascade remove option, after removing User or Office, the PhoneNumber remains in the Database (but the record in mapping table is removed).

What is the best way to handle this type of association?

Thanks

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

    I finally solve the problem connected with (probably the nicest) solution 1). The problem was in misunderstanding of mappedBy attribute which should specify the entity field, not the database field.

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

Sidebar

Related Questions

I have three related entities: a user has many device a user has a
I have three entities: Session, User and Test. A session has 0-many users and
I have three entities A, B, C. A has many Bs and a C,
I have three entities: User , Team , and TeamInvite . Each User has
I have three entities: EntityA, EntityB and EntityC connected with to-many relationships. See schema
I have a case where i have three entities with one-to-many and one-to-many relationships:
Have three classes User, Group and Field. Many to many relationship on User /
I have three entities that must interact: User , SupportTicket and PhoneConversation . When
Got three entities - User - Has username/password, contact information, billing information etc. Periodical
I have three entities: User , Answer , and Question . There is a

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.