propelorm / Propel2

Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP
http://propelorm.org/
MIT License
1.26k stars 399 forks source link

Create IN filter expressions for subqueries #1920

Closed mringler closed 1 year ago

mringler commented 1 year ago

This allows to create IN queries like WHERE column IN (SELECT otherColumn FROM ...).

It can be done by using the filterBy() methods:

$innerQuery = BookQuery::create()
  ->filterByTitle('%Galaxy%', Criteria::LIKE)
  ->select('author_id');
AuthorQuery::create()
  ->filterById($innerQuery)
  ->find();

which will generate a statement like:

SELECT * 
FROM author 
WHERE id IN (
  SELECT author_id
  FROM book
  WHERE book.title LIKE '%Galaxy%'
)

For known relations, there are useInQuery() methods which work similar as the useQuery() methods:

AuthorQuery::create()
  ->useInQuery('Book')
    ->filterByTitle('%Galaxy%', Criteria::LIKE)
  ->endUse()
  ->find();

Propel creates convenience methods for existing relations on query classes like AuthorQuery::useInBookQuery(), which is easier to use than useInQuery('Book').

NOT IN queries are created by passing CRITERIA::NOT_IN to filterBy() or the useNotInQuery() method.

I have build a similar thing for EXISTS, so this is mostly just abstracting from that.

codecov-commenter commented 1 year ago

Codecov Report

Base: 73.25% // Head: 88.34% // Increases project coverage by +15.08% :tada:

Coverage data is based on head (9bb2923) compared to base (b542279). Patch coverage: 92.24% of modified lines in pull request are covered.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1920 +/- ## ============================================= + Coverage 73.25% 88.34% +15.08% - Complexity 7917 7942 +25 ============================================= Files 227 230 +3 Lines 21133 21201 +68 ============================================= + Hits 15480 18729 +3249 + Misses 5653 2472 -3181 ``` | Flag | Coverage Δ | | |---|---|---| | 5-max | `88.34% <92.24%> (+15.08%)` | :arrow_up: | | 7.4 | `88.34% <92.24%> (+15.08%)` | :arrow_up: | | agnostic | `67.38% <62.06%> (?)` | | | mysql | `68.99% <92.24%> (+0.03%)` | :arrow_up: | | pgsql | `69.08% <92.24%> (+0.03%)` | :arrow_up: | | sqlite | `67.00% <92.24%> (+0.04%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#carryforward-flags-in-the-pull-request-comment) to find out more. | [Impacted Files](https://codecov.io/gh/propelorm/Propel2/pull/1920?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm) | Coverage Δ | | |---|---|---| | [...l/Generator/Behavior/Delegate/DelegateBehavior.php](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#diff-c3JjL1Byb3BlbC9HZW5lcmF0b3IvQmVoYXZpb3IvRGVsZWdhdGUvRGVsZWdhdGVCZWhhdmlvci5waHA=) | `93.02% <ø> (+93.02%)` | :arrow_up: | | [.../Runtime/ActiveQuery/Criterion/ExistsCriterion.php](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#diff-c3JjL1Byb3BlbC9SdW50aW1lL0FjdGl2ZVF1ZXJ5L0NyaXRlcmlvbi9FeGlzdHNDcml0ZXJpb24ucGhw) | `0.00% <0.00%> (-100.00%)` | :arrow_down: | | [src/Propel/Runtime/ActiveQuery/ModelCriteria.php](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#diff-c3JjL1Byb3BlbC9SdW50aW1lL0FjdGl2ZVF1ZXJ5L01vZGVsQ3JpdGVyaWEucGhw) | `95.79% <89.18%> (+0.87%)` | :arrow_up: | | [src/Propel/Runtime/ActiveQuery/Criteria.php](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#diff-c3JjL1Byb3BlbC9SdW50aW1lL0FjdGl2ZVF1ZXJ5L0NyaXRlcmlhLnBocA==) | `87.88% <90.90%> (+4.58%)` | :arrow_up: | | [...iveQuery/Criterion/AbstractInnerQueryCriterion.php](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#diff-c3JjL1Byb3BlbC9SdW50aW1lL0FjdGl2ZVF1ZXJ5L0NyaXRlcmlvbi9BYnN0cmFjdElubmVyUXVlcnlDcml0ZXJpb24ucGhw) | `96.15% <96.15%> (ø)` | | | [src/Propel/Generator/Builder/Om/QueryBuilder.php](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#diff-c3JjL1Byb3BlbC9HZW5lcmF0b3IvQnVpbGRlci9PbS9RdWVyeUJ1aWxkZXIucGhw) | `90.11% <100.00%> (+1.46%)` | :arrow_up: | | [...ime/ActiveQuery/Criterion/ExistsQueryCriterion.php](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#diff-c3JjL1Byb3BlbC9SdW50aW1lL0FjdGl2ZVF1ZXJ5L0NyaXRlcmlvbi9FeGlzdHNRdWVyeUNyaXRlcmlvbi5waHA=) | `100.00% <100.00%> (ø)` | | | [...Runtime/ActiveQuery/Criterion/InQueryCriterion.php](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#diff-c3JjL1Byb3BlbC9SdW50aW1lL0FjdGl2ZVF1ZXJ5L0NyaXRlcmlvbi9JblF1ZXJ5Q3JpdGVyaW9uLnBocA==) | `100.00% <100.00%> (ø)` | | | [...nerator/Builder/Om/TableMapLoaderScriptBuilder.php](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm#diff-c3JjL1Byb3BlbC9HZW5lcmF0b3IvQnVpbGRlci9PbS9UYWJsZU1hcExvYWRlclNjcmlwdEJ1aWxkZXIucGhw) | `95.12% <0.00%> (-2.38%)` | :arrow_down: | | ... and [125 more](https://codecov.io/gh/propelorm/Propel2/pull/1920/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm) | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=propelorm)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

mringler commented 1 year ago

PR for documentation is at propelorm/propelorm.github.com#429