-
Notifications
You must be signed in to change notification settings - Fork 103
Keep QueryBuilderType across method calls #266
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
Comments
Hi, AFAIK this is what phpstan-doctrine/extension.neon Line 9 in 938350f
Feel free to pair it with Line 3 in 938350f
|
OK, my original error was obviously more complex than this example and has been resolved with version 1.2.7. So thank you very much for that. /**
* @return array<Entity>
*/
public function reuseQueryBuilder(): array
{
return $this->getQueryBuilder()
->andWhere('test.foo = 1')
->getQuery()
->getResult();
}
private function getQueryBuilder(): QueryBuilder
{
return $this->createQueryBuilder('test');
} |
After seeing the tweet here. I tried to give it a try on local project. I think I'm running into the same issue: Which Repositories look like this: https://github.com/alexander-schranz/hexagonal-architecture-study/blob/e0b6cd46085e0cb548d02e648ad6d86255e9c301/src/event/src/Infrastructure/ORM/Doctrine/Repository/EventRepository.php#L136 In the findOneBy the result is still $queryBuilder = $this->createQueryBuilder($filters);
$query = $queryBuilder->getQuery();
$result = $query->getResult();
\PHPStan\dumpType($result); I tried to add a generic to the method return type e.g.: * @return QueryBuilder<MyEntity> But this ends still in mixed and a generic error: 60 Dumped type: mixed
119 PHPDoc tag @return contains generic type Doctrine\ORM\QueryBuilder<App\Event\Domain\Model\Event> but class Doctrine\ORM\QueryBuilder is not generic. The Could not analyse QueryBuilder with unknown beginning. I think it would be nice if Maybe something which could be added to the doctrine codebase, not sure if the ORM QueryBuilder/Query can be a generic. /cc @derrabus More details phpstan.neon, install versions, ...My Package Versions related to phpstan:
My phpstan.neon
My <?php
declare(strict_types=1);
use App\Kernel;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Events;
use Doctrine\ORM\Tools\ResolveTargetEntityListener;
use Symfony\Component\DependencyInjection\ContainerInterface;
require \dirname(__DIR__) . '/bootstrap.php';
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$kernel->boot();
/** @var ContainerInterface $container */
$container = $kernel->getContainer();
/** @var EntityManager $objectManager */
$objectManager = $container->get('doctrine')->getManager();
// remove ResolveTargetEntityListener from returned EntityManager to not resolve SuluPersistenceBundle classes
// this is a workaround for the following phpstan issue: https://github.com/phpstan/phpstan-doctrine/issues/98
$resolveTargetEntityListener = current(array_filter(
$objectManager->getEventManager()->getListeners('loadClassMetadata'),
static function ($listener) {
return $listener instanceof ResolveTargetEntityListener;
}
));
if ($resolveTargetEntityListener) {
$objectManager->getEventManager()->removeEventListener([Events::loadClassMetadata], $resolveTargetEntityListener);
}
return $objectManager; |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I don't know if this is currently even possible, but it would be nice, if Phpstan could determine the type of the QueryBuilder, even if it is defined in another method. Currently something like this results in a mixed return type:
The text was updated successfully, but these errors were encountered: