Skip to content

Function IDENTITY() loses null type #293

Closed
@jlherren

Description

@jlherren

Given this Doctrine setup (see below for details):

  • Supplier has a nullable many-to-one to Country
  • Country has a non-nullable many-to-one to Continent

And this query:

$rows = $this->em->createQueryBuilder()
    ->select(
        'supplier.id AS supplierId',
        'IDENTITY(country.continent) AS continentId1', 
        'continent.id AS continentId2',
    )
    ->from(Supplier::class, 'supplier')
    ->leftJoin('supplier.country', 'country')
    ->leftJoin('country.continent', 'continent')
    ->getQuery()
    ->getResult();
\PHPStan\dumpType($rows);

It dumps the following type:

Dumped type: array<array{supplierId: int, continentId1: int|numeric-string, continentId2: int|null}>

The continent ID retrieved using IDENTITY(country.continent) is not nullable, but it should be nullable due to suppliers not necessarily having a country. (I'm less concerned about the numeric-string.)

Details about entities used Supplier:
use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Supplier {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    public int $id;

    /**
     * @ORM\ManyToOne(targetEntity="Country")
     * @ORM\JoinColumn(nullable=true)
     */
    public ?Country $country;
}

Country

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Country {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\ManyToOne(targetEntity="Continent")
     * @ORM\JoinColumn(nullable=false)
     */
    public Continent $continent;
}

Continent

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Continent {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\Column(type="string")
     */
    public string $name;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions