Skip to content

Wrong type inference for QueryBuilder::update and QueryBuilder::delete. #259

Closed
@VincentLanglet

Description

@VincentLanglet

The following code

$result = $this->getEntityManager()
            ->getRepository(ProductCrossSell::class)
            ->createQueryBuilder('pcs')
            ->where('pcs.id IN (:ids)')
            ->setParameter('ids', $ids)
            ->delete()
            ->getQuery()
            ->getResult();

\PHPStan\dumpType($result);

Is considering $result as an array. But it's an int: the number of deleted elements. Same with update()

It was introduce by #232 cc @arnaud-lb

Activity

arnaud-lb

arnaud-lb commented on Jan 24, 2022

@arnaud-lb
Contributor

Hi @VincentLanglet

I believe that there are two distinct issues here. The first one is that

$result = $this->getEntityManager()
            ->getRepository(ProductCrossSell::class)
            ->createQueryBuilder('pcs')
            ->where('pcs.id IN (:ids)')
            ->setParameter('ids', $ids)
            ->delete()
            ->getQuery()

may be Query<mixed>. Could you confirm ?

If that's the case, the second issue is that the type of Query<mixed>::getResult() should not be array because the query may be an INSERT or DELETE query.

VincentLanglet

VincentLanglet commented on Jan 24, 2022

@VincentLanglet
ContributorAuthor

may be Query<mixed>. Could you confirm ?

Indeed Dumped type: Doctrine\ORM\Query<mixed>

arnaud-lb

arnaud-lb commented on Jan 24, 2022

@arnaud-lb
Contributor

Thank you.

I'm fixing the second issue in #260.

I'm not sure about the cause of the first issue. What is the type of ->getRepository(ProductCrossSell::class) ? Could you confirm that the query is valid ?

VincentLanglet

VincentLanglet commented on Jan 24, 2022

@VincentLanglet
ContributorAuthor

I'm not sure about the cause of the first issue. What is the type of ->getRepository(ProductCrossSell::class) ?

Dumped type: App\Repository\ProductCrossSellRepository<App\Entity\ProductCrossSell>  

Could you confirm that the query is valid ?

Yes, it's just a DELETE with a WHERE condition on ids.

github-actions

github-actions commented on Feb 25, 2022

@github-actions

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.

locked as resolved and limited conversation to collaborators on Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @arnaud-lb@VincentLanglet

      Issue actions

        Wrong type inference for QueryBuilder::update and QueryBuilder::delete. · Issue #259 · phpstan/phpstan-doctrine