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

[RFC] Expose QueryBuilder::getType #11813

Merged
merged 1 commit into from
Jan 29, 2025
Merged

Conversation

VincentLanglet
Copy link
Contributor

I have an implementation where I extends the QueryBuilder in order to provide extra methods.
In one of them, I'd like an easy way to know if the queryBuilder is creating a select, an update or a delete query.

I could extends select, addSelect, update and delete to keep a state with the type of the current query, but it seems like the QueryBuilder already does this. So I'd like to expose this information.

Are you okay with this ?

@derrabus
Copy link
Member

Sounds reasonable to me. What do the others think?

Copy link
Member

@greg0ire greg0ire left a comment

Choose a reason for hiding this comment

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

Can you show us the piece of code where you need this, in case we can see another way?

@@ -128,6 +127,11 @@ public function __construct(
$this->parameters = new ArrayCollection();
}

public function getType(): QueryType
Copy link
Member

Choose a reason for hiding this comment

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

Let's not open this too much, shall we?

Suggested change
public function getType(): QueryType
final protected function getType(): QueryType

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I remove the test then ?

Copy link
Member

Choose a reason for hiding this comment

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

This is getting philosophical… I think protected method are part of the public API, so you should keep testing them. You could have

$qb = new class  extends QueryBuilder
{
    public method assertCallingUpdateMakesTheQueryAnUpdate(): void
    {
        $this->update();
        TestCase::assertSame(QueryType::UPDATE, $this->getType());
    }
}

$qb->assertCallingUpdateMakesTheQueryAnUpdate();

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Jan 29, 2025

Can you show us the piece of code where you need this, in case we can see another way?

We have a method to batch Queries when the array of element is too big / the query too slow.
The implementation is basically this one.

chunkQueryAndExecute(string $parameterToChunk, int $chunkSize = 1000, int|string|null $hydrationMode = null)
{
     $parameter = $this->getParameter($parameterToChunk);
     $chunks = array_chunk($parameter->getValue(), $chunkSize, true);
     foreach ($chunks as $chunk) {
          $this->setParameter($parameterToChunk, $chunk);
          $result = $this->getQuery()->execute(null, $hydrationMode);
     }
}

Now I'd like to get the result.

  • For select queries, we need to array merge all the results.
  • For update/delete queries, we need to sum all the results.

Also, technically I should handle the case where $parameter->getValue() is an empty array and so without result, I cannot know if I need to return [] or 0.

I could use getDQLParts['select'] to check if there is some but

  • I really feel hacky to play with DQLParts when there is an existing type.
  • Technically, if I do $qb->select('foo')->delete() I think it will be a Type::Delete with a select dql part.

@greg0ire
Copy link
Member

Please squash

@VincentLanglet
Copy link
Contributor Author

Please squash

Sure, done

@greg0ire greg0ire added this to the 3.4.0 milestone Jan 29, 2025
@greg0ire greg0ire merged commit 4163efd into doctrine:3.4.x Jan 29, 2025
84 checks passed
@greg0ire
Copy link
Member

Thanks @VincentLanglet !

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

Successfully merging this pull request may close these issues.

3 participants