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

Fix SQL alias generation regression for simple inheritance #8329

Merged
merged 1 commit into from
Nov 10, 2020

Conversation

rogregoire
Copy link
Contributor

As asked by @ostrolucky in the #8229 issue - I'm created a PR trying to fix a edge case of what I think may be a regression in the effort of supporting Joined table inheritance with AttributeOverride.

Context:

  • Entity1 extends Entity0 through ResolveTargetEntities mecanism
  • Entity1 has all its fieldsMapping marked as inherited=true
  • Entity1 inheritanceType is ClassMetadata::INHERITANCE_TYPE_NONE
  • Some code calls $entityManager->getRepository($entity1)->findOneBy()
  • The SQL generated in the where part of the query has a bad alias because it uses Entity0 as classname for getSQLTableAlias call in BasicEntityPersister::getSelectConditionStatementSQL method
  • The query crash

Proposal:
Excluding INHERITANCE_TYPE_NONE entities to use inherited classname as parameter for getSQLTableAlias generation in BasicEntityPersister::getSelectConditionStatementSQL

As said in original comment, I'm really not sure of what I'm doing, I'm new to doctrine 2

Referenced Issue comments: #8229 (comment)

@ostrolucky
Copy link
Member

Test case is also needed

This fixes a regression from 099c5b4.
Without the fix, "where part" in SQL is generated with incorrect aliases.
See doctrine#8229 (comment).
@ostrolucky
Copy link
Member

There is lot of problems in this PR. Please edit this PR and check this checkbox, so I can help you.

Screenshot 2020-11-09 at 18 53 04

@ostrolucky
Copy link
Member

Have you checked the checkbox yet?

@rogregoire
Copy link
Contributor Author

image
That's all I found and it's already checked, Am I missing something ?

@ostrolucky ostrolucky changed the title Trying to fix a regression in SQL "Where part" generation for inheri… Fix SQL alias generation regression for simple inheritance Nov 10, 2020
@ostrolucky
Copy link
Member

ostrolucky commented Nov 10, 2020

I have cleaned up git history, removed unnecessary try catch in a test, unnecessary usage of ResolveTargetEntityListener and renamed entities you created. The listener has no effect on reproducing the issue.

If you plan to push some more commits, you need to pull with rebase your branch: git pull --rebase

@rogregoire
Copy link
Contributor Author

Thanks,
Well, I didn't test without ResolveTargetEntityListener because that wasn't my bug case, but I guess that mean this bug must have impacted a lot more of people that I initially thought ?

@ostrolucky
Copy link
Member

yes

@ostrolucky ostrolucky merged commit f4ebded into doctrine:2.7 Nov 10, 2020
@ostrolucky ostrolucky added this to the 2.7.5 milestone Nov 10, 2020
@cziegenberg
Copy link
Contributor

cziegenberg commented Nov 10, 2020

Thanks @rogregoire!

FYI: I just found two other untested use cases for which my #8229 fix doesn't work yet. I'm currently trying to create tests for it and will provide a PR soon.

@ostrolucky
Copy link
Member

Thanks! Ping me in a PR afterwads

@cziegenberg
Copy link
Contributor

@ostrolucky After some testing everything works fine. The problem was an outdated composer.lock file not matching the composer.json file and pointing to an old version.

@ostrolucky
Copy link
Member

👍

@beberlei
Copy link
Member

beberlei commented Nov 23, 2020

@cziegenberg can you please explain your use case? This looks like invalid mapping to me. But maybe I am wrong, are these two entities stored in one or two distinct tables in your case?

I am pretty sure this fails in many other places that only check for 'inherited' and nothing more. I believe this use-case is not actually supported by Doctrine reliably. Not sure if we should support this... The better approach would be to have a mapped superclass as the base, and then have two entities without properties extend from it.

@beberlei
Copy link
Member

@rogregoire The ResolveEntityTargetListener does not work when you extend another entity with. Its specifically for implementing an interface. Your use-case of Entity1 extends Entity0 is only supported if you do Inheritence Mapping.

@cziegenberg
Copy link
Contributor

@beberlei:

This isn't my PR, so do you really mean my usecase? My use case is explained in #8229. The goal is a "everything is a resource"-structure:

  • a global resource table "app_resource" with the columns "resource_id" and "resource_type"
  • an own table for each specific resource type (i.e. "app_user") with additional, type-specific columns, and an adjusted name for the identifier column to clearly name it and differ references
  • other tables referencing either any type of resource (-> "app_resource") or a specific one (i.e. "app_user")

A mapped super class wouldn't allow to reference "any type of resource", right?

beberlei added a commit to beberlei/doctrine2 that referenced this pull request Nov 24, 2020
beberlei added a commit that referenced this pull request Nov 25, 2020
* [GH-8229] Prevent AttributeOverride on fields from entities, only allowed for MappedSuperclass

* [GH-8229] Prevent AssociationOverride on fields from entities, only allowed for MappedSuperclass

* Revert "Fix SQL alias generation regression for simple inheritance (#8329)"

This reverts commit f4ebded.

* [GH-8229] Finalize checks for illegal attribute/assocation overrides.

* [GH-8229] Revert ccae8f7 PR #8234

* [GH-8229] Update documentation to clarify only mapped superclass or trait works with overrides

* [GH-8229] Fix style violations introduced by revert

* [GH-8229] Fix style violations introduced by revert

* [GH-8229] Temporarily disable the exception until 2.8.

* Make phpcs happy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants