[PR #11406] Psalmify QueryBuilder, Query and EntityRepository. #13003

Open
opened 2026-01-22 16:15:51 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/11406

State: closed
Merged: No


Added templates for QueryBuilder, Query and EntityRepository.


With repository defined like:

/** 
 * @extends EntityRepository<Product> 
 */
class ProductRepository extends EntityRepository{}

Using it:

$qb = $productRepository->createQueryBuilder('p'); // QueryBuilder<Product>
$query = $qb->getQuery(); // Query<Product>

$query->getResults(); // array<array-key, Product>
$query->getOneOrNullResult(); // Product|null
$query->getSingleResult(); // Product

If at any point user makes a call to QueryBuilder::select, it will be converted to mixed via @psalm-this-out self<mixed>. For example:

$qb = $productRepository->createQueryBuilder('p'); // QueryBuilder<Product>
$qb->select('p.name'); // QueryBuilder<mixed>
$query = $qb->getQuery(); // Query<mixed>

$query->getResults(); // mixed

The reasoning is that almost all queries are done with object hydration and this would help in later processing. Custom select will break out of static analysis and it is up to user to assert returned results.

**Original Pull Request:** https://github.com/doctrine/orm/pull/11406 **State:** closed **Merged:** No --- Added templates for QueryBuilder, Query and EntityRepository. --- With repository defined like: ```php /** * @extends EntityRepository<Product> */ class ProductRepository extends EntityRepository{} ``` Using it: ```php $qb = $productRepository->createQueryBuilder('p'); // QueryBuilder<Product> $query = $qb->getQuery(); // Query<Product> $query->getResults(); // array<array-key, Product> $query->getOneOrNullResult(); // Product|null $query->getSingleResult(); // Product ``` If at any point user makes a call to `QueryBuilder::select`, it will be converted to `mixed` via `@psalm-this-out self<mixed>`. For example: ```php $qb = $productRepository->createQueryBuilder('p'); // QueryBuilder<Product> $qb->select('p.name'); // QueryBuilder<mixed> $query = $qb->getQuery(); // Query<mixed> $query->getResults(); // mixed ``` --- The reasoning is that almost all queries are done with object hydration and this would help in later processing. Custom `select` will break out of static analysis and it is up to user to [assert](https://github.com/webmozarts/assert) returned results.
admin added the pull-request label 2026-01-22 16:15:51 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#13003