Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDC-2772: One class can be mapped to multiple discriminator values #3519

Open
doctrinebot opened this issue Nov 1, 2013 · 6 comments
Open
Assignees
Labels

Comments

@doctrinebot
Copy link

Jira issue originally created by user mouyang:

I'm not sure if this is intentional or not, but this appears to a valid discriminator map. The problem I am running into is that it's impossible to create an instance of ClassB with a discriminator value "DiscB" (it's always "DiscC" - I'm assuming last one wins here).

<entity name="ClassB" ...>
  <discriminator-map>
    <discriminator-mapping value="DiscA" class="ClassA" />
    <discriminator-mapping value="DiscB" class="ClassB" />
    <discriminator-mapping value="DiscC" class="ClassB" />
  </discriminator-map>
</entity>

Eventhough DiscB and DiscC map to the same class, I do want to maintain some sort of logical separation in the database using the two discriminator values. That does not appear to be possible. I can provide more details about my data model, if necessary.

These are the possible solutions I can think of.

  1. Enforce a strict one-to-one relationship between value and class. This means I would have to modify my code to reflect this.
  2. (I know this has been rejected before, but I'm in favour of this solution.) Expose the discriminator field but validate it before committing to the database. I'm actually using the discriminator as part of a compound primary key, so this would be very helpful to me.
@guilhermeblanco
Copy link
Member

Our intended solution is 1. "Enforce a strict one-to-one relationship between value and class. This means I would have to modify my code to reflect this."

An enforcement should be added restricting people to map multiple discriminator values to a single class. This should be done at mapping level by checking count(array_unique($map)) === count($map) and adding a MappingException if it doesn't match.

@beberlei
Copy link
Member

This makes sense for ORM 3.0 but also requires a deprecation message to be thrown in 2.x

@derrabus
Copy link
Member

Do you want to work on it? 🙃

@javijavo
Copy link

javijavo commented May 13, 2022

Hello,
I would like to ask why is this not possible?
If I have a let's say two possible classes Mammals and Reptiles and I want to discriminate "cat", "dog" to Mammals and "turtle" to Reptile? How do we do it after this deprecation?

(The example is very basic but I have some entity that works thay way)

Thanks

@beberlei
Copy link
Member

@javijavo but there is no way to new Mamal() and have it stored as either dog or cat. Do you create these entities outside the ORM?

@javijavo
Copy link

I have a very old and huge legacy application progressively adating it to Doctrine and yes the data is already stored.

But I see the problem of having multiple values, didn't have in mind the "standard" scenario instead my ugly one.
Will look for another way to meet my requirements.

Thanks

@greg0ire greg0ire removed this from the 3.0.0 milestone Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants