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

Connection to the database is triggered #292

Closed
gboutte opened this issue Feb 17, 2022 · 3 comments
Closed

Connection to the database is triggered #292

gboutte opened this issue Feb 17, 2022 · 3 comments

Comments

@gboutte
Copy link

gboutte commented Feb 17, 2022

I'm not sure i should post here, let me know if it's not the right place.

I am using phpstan 1.4 and phpstan-doctrine 1.2.

When i run phpstan vendor/bin/phpstan analyze it appears that phpstan is trying to connect to the database, but it's not accessible.

Internal error: An exception occurred in the driver: SQLSTATE[08006] [7] could not translate host name "postgres_db" to address: nodename nor servname provided, or not known

I know that it's when there is a getRepository because when i comment it i don't have the error.

$this->userRepository = $this->entityManager->getRepository(User::class);

I ran phpstan with the --debug flag, i have 3 lines that are about phpstan-doctrine, the rest is inside phpstan files or doctrine files.

// Doctrine part

#12 /Users/gboutte/myproject/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/ObjectMetadataResolver.php(131): Doctrine\ORM\EntityManager->getClassMetadata('App\\Entities\\Us...')
#13 /Users/gboutte/myproject/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/GetRepositoryDynamicReturnTypeExtension.php(153): PHPStan\Type\Doctrine\ObjectMetadataResolver->getClassMetadata('App\\Entities\\Us...')
#14 /Users/gboutte/myproject/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/GetRepositoryDynamicReturnTypeExtension.php(112): PHPStan\Type\Doctrine\GetRepositoryDynamicReturnTypeExtension->getRepositoryClass('App\\Entities\\Us...', 'Doctrine\\ORM\\En...')

// Phpstan part

If i look at src/Type/Doctrine/ObjectMetadataResolver.php line 131

try {
if ($objectManager === null) {
$metadataFactory = $this->getMetadataFactory();
if ($metadataFactory === null) {
return null;
}
$metadata = $metadataFactory->getMetadataFor($className);
} else {
$metadata = $objectManager->getClassMetadata($className);
}
} catch (\Doctrine\Persistence\Mapping\MappingException | MappingException | AnnotationException $e) {
return null;
}

the $metadata = $objectManager->getClassMetadata($className); is triggering the connection.
I tried to remove the condition and always do the if part:

$metadataFactory = $this->getMetadataFactory();
if ($metadataFactory === null) {
	return null;
}

$metadata = $metadataFactory->getMetadataFor($className);

With that changes i don't have the error anymore, but i don't know exactly what was the purpose of that line.

Does phpstan need to connect to the database ? What do you think about this ?

@jlherren
Copy link

Hi! I might be completely wrong here, but it's worth a shot.

I had a similar issue with Doctrine connecting to MariaDB, when I wasn't doing any queries at all. The problem is that Doctrine needs to know what version of the DB server it is working with in order to instantiate the correct AbstractPlatform implementation, of which there are several available for the same DB vendor (e.g. PostgreSQL94Platform or PostgreSQL100Platform for postgres, and similarly for other DB drivers). To auto-detect this information, it will simply connect to the DB and query the version.

To turn off this auto-detection, you can specify this information in advance, like so:

$params = [
    'host' => 'localhost',
    'user' => 'dbuser',
    ...
    'serverVersion' => '14.2',
];

@gboutte
Copy link
Author

gboutte commented Feb 21, 2022

@jlherren You were right, it was caused by the serverVersion, I added it and I no longer have the error.

Since i'm using an url I added it on my url:

DATABASE_URL=postgres://user:password@database_ip/database?serverVersion=14.2
$params = [
    'url' => $database_url,
];

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants