Skip to content

Conversation

@nacholibre
Copy link

Q A
Type feature

Summary

Quite often I find myself needing async queries in doctrine/orm. In order to implement them first dbal needs to have that functionality.

This PR is me prototyping (consider it proof of concept) for async functionality. Just to note I've used some AI tools to help me prototype and understand the codebase.

I don't consider this as finished product, I just want to get some feedback from the maintainers if my approach is in the right direction and general thoughts about the need of such feature.

The API is as follows:

// Create queries using QueryBuilder
$qb1 = $conn->createQueryBuilder()
    ->select('*')
    ->from('users')
    ->where('status = :status')
    ->setParameter('status', 'active');

$qb2 = $conn->createQueryBuilder()
    ->select('COUNT(*)')
    ->from('orders')
    ->where('created_at > :date')
    ->setParameter('date', '2024-01-01');

// Execute in parallel using the helper method
$results = $conn->executeQueriesAsync([
    AsyncQuery::fromQueryBuilder($qb1),
    AsyncQuery::fromQueryBuilder($qb2),
]);

$users = $results[0]->fetchAllAssociative();
$orderCount = $results[1]->fetchOne();

It works without the QueryBuilder as well

// Execute multiple slow queries in parallel
$results = $connection->executeQueriesAsync([
    new AsyncQuery('SELECT * FROM users WHERE status = $1', ['active']),
    new AsyncQuery('SELECT COUNT(*) as total FROM orders'),
    new AsyncQuery('SELECT AVG(price) as avg_price FROM products WHERE category_id = $1', [5]),
]);

Thanks for your time.

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.

1 participant