Skip to content

Create IN filter expressions for subqueries#1920

Merged
dereuromark merged 5 commits intopropelorm:masterfrom
perplorm:feature/in_query_filter
Dec 19, 2022
Merged

Create IN filter expressions for subqueries#1920
dereuromark merged 5 commits intopropelorm:masterfrom
perplorm:feature/in_query_filter

Conversation

@mringler
Copy link
Contributor

@mringler mringler commented Dec 8, 2022

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.

@mringler mringler force-pushed the feature/in_query_filter branch from 36eaf51 to 19cd3ab Compare December 8, 2022 22:20
@codecov-commenter
Copy link

codecov-commenter commented Dec 9, 2022

Codecov Report

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

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
@@              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%) ⬆️
7.4 88.34% <92.24%> (+15.08%) ⬆️
agnostic 67.38% <62.06%> (?)
mysql 68.99% <92.24%> (+0.03%) ⬆️
pgsql 69.08% <92.24%> (+0.03%) ⬆️
sqlite 67.00% <92.24%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...l/Generator/Behavior/Delegate/DelegateBehavior.php 93.02% <ø> (+93.02%) ⬆️
.../Runtime/ActiveQuery/Criterion/ExistsCriterion.php 0.00% <0.00%> (-100.00%) ⬇️
src/Propel/Runtime/ActiveQuery/ModelCriteria.php 95.79% <89.18%> (+0.87%) ⬆️
src/Propel/Runtime/ActiveQuery/Criteria.php 87.88% <90.90%> (+4.58%) ⬆️
...iveQuery/Criterion/AbstractInnerQueryCriterion.php 96.15% <96.15%> (ø)
src/Propel/Generator/Builder/Om/QueryBuilder.php 90.11% <100.00%> (+1.46%) ⬆️
...ime/ActiveQuery/Criterion/ExistsQueryCriterion.php 100.00% <100.00%> (ø)
...Runtime/ActiveQuery/Criterion/InQueryCriterion.php 100.00% <100.00%> (ø)
...nerator/Builder/Om/TableMapLoaderScriptBuilder.php 95.12% <0.00%> (-2.38%) ⬇️
... and 125 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

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

Copy link

@asmarovydlo asmarovydlo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job 👍

@mringler
Copy link
Contributor Author

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

@dereuromark dereuromark merged commit ca04ce0 into propelorm:master Dec 19, 2022
@mringler mringler deleted the feature/in_query_filter branch December 19, 2022 11:20
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