Note that there are some explanatory texts on larger screens.

plurals
  1. POOne-to-many relation ship between 3 entities in Doctrine 2
    text
    copied!<p>I have three entities: User, Office and PhoneNumber. The user has many phone numbers, and the office has many phone numbers too.</p> <p>The problem is how to represent these entities relations in Doctrine 2. </p> <ol> <li><p>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.</p></li> <li><p>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) </p> <p>This solution actually works, but it is quite ugly to have 3 classes to represent one simple entity.</p></li> <li><p>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. </p> <p>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). </p></li> </ol> <p>What is the best way to handle this type of association?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload