question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doctrine TypeProvider does not work for class fields

See original GitHub issue

Hi all! In the following example the type provider fails to recognize types when calling getManager() on doctrine. A call to the manager is often necessary as there might be multiple of them.

Something to note is that on the following example all of the cases will work if one would use directly the $registry argument rather than the class field. Also the RegistryInterface is not in fact the default but rather a custom one inheriting Symfony\Bridge\Doctrine\RegistryInterface and overriding the default @doctrine service.

class SomeService
{
    /** @var RegistryInterface */
    private $registry;
    
    public function __construct(RegistryInterface $registry) {
        $this->registry = $registry;
        
        $works = $this->registry
            ->getRepository(Customer::class)
            ->find(1);

        $doesntWork = $this->registry
            ->getManager()
            ->getRepository(Customer::class)
            ->findBy(1);

        $neitherDoesThis = $this->registry
            ->getManager('main')
            ->getRepository(Customer::class)
            ->find(1);
    }
}

Additional info:

Item Version
PHP 7.3.13
Plugin 0.20.198
PhpStorm Build #PS-201.7846.90, built on June 3, 2020

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:12

github_iconTop GitHub Comments

1reaction
zhilcommented, Oct 22, 2020

Looks like I have this bug in PhpStorm 2020.2 with symfony plugin 0.21.203 I have entity Hospital, and SomeCommand.php

Sample code

        $hospitals = $this->em->getRepository(Hospital::class)->findBy(['oec_autoSchedule_enable' => true]);
        foreach ($hospitals as $hospital) {
            $hospitalDate = $hospital->getHospitalTimeDate();
        }

Hospital class is resolved, findBy method signature seems to be resolved (check gif), but $hospitals have type object[] 👍

phpstorm

findAll() / find($x) - doesnt work as well.

Any ideas how can I fix it without adding

/** @var $hospital Hospital */

?

1reaction
maldoinccommented, Aug 12, 2020

It does seem to be fixed. However the strangest bug appears where nested objects are no longer able to be autocompleted even though their type is known. Probably a phpstorm issue though.

Does that happen for you @Jasonoro ?

$res = $this->getDoctrine()->getManager()->getRepository(Contact::class)->find(1);
$res->getCreatedAt()->format('d.m.Y');

In the above example the IDE is no longer able to autocomplete the DateTime methods from getCreatedAt. When type-hinting the res variable with a docblock everything works properly again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not resolve type of column Symfony - Stack Overflow
I want to achieve a join between my product entity and my provider entity, only, all seems ok but it does not work...
Read more >
Timestamp is not a valid Doctrine column type #35498 - GitHub
Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with...
Read more >
Working with Objects - Doctrine
DQL enables you to query for persistent objects in the language of objects. DQL understands classes, fields, inheritance and associations. DQL is syntactically ......
Read more >
Forms (Symfony Docs)
The recommended workflow when working with Symfony forms is the following: Build the form in a Symfony controller or using a dedicated form...
Read more >
Doctrine 2 ORM Documentation - Read the Docs
This guide covers getting started with the Doctrine ORM. After working through the guide you should know: • How to install and configure ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found