Skip to content

Not nullable return type on entities property which are used by primary could throw TypeError Exception #39

@robotccm

Description

@robotccm

Original report by Nicolas Desaleux (Bitbucket: [Nicolas Desaleux](https://bitbucket.org/Nicolas Desaleux), ).


Since PHP 7.0+, return type hinting is allowed.

To test, this error, you will add this test on Hydrator test suite

    public function testHydrateWithEntityHavingReturnTypeOnPrimaryKey()
    {
        $services = new \CCMBenchmark\Ting\Services();
        $services->get('MetadataRepository')
            ->batchLoadMetadata('tests\fixtures\model', __DIR__ . '/../../../fixtures/model/*Repository.php');

        $mockMysqliResult = new \mock\tests\fixtures\FakeDriver\MysqliResult([
             ['Palaiseau']
         ]);

        $this->calling($mockMysqliResult)->fetch_fields = function () {
            $fields = [];

            $stdClass = new \stdClass();
            $stdClass->name     = 'citname';
            $stdClass->orgname  = 'cit_name';
            $stdClass->table    = 'cit';
            $stdClass->orgtable = 'T_CITY_CIT';
            $stdClass->type     = MYSQLI_TYPE_VAR_STRING;
            $fields[] = $stdClass;
            return $fields;
        };

        $result = new Result();
        $result->setResult($mockMysqliResult);
        $result->setConnectionName('main');
        $result->setDatabase('bouh_world');

        $this
            ->if($hydrator = new \CCMBenchmark\Ting\Repository\Hydrator())
                ->and($hydrator->setMetadataRepository($services->get('MetadataRepository')))
                ->and($hydrator->setUnitOfWork($services->get('UnitOfWork')))
            ->then($result = iterator_to_array($hydrator->setResult($result)->getIterator()))
                ->integer(count($result))
                    ->isEqualTo(1);
    }

And add into entity City a return type to getId() like :

    public function getId(): int
    {
        return $this->id;
    }

To avoid this error, you have 2 solutions :

  • Return type should be nullable on primary key (allowed since PHP 7.1)
  • or add missing primary key into your select request

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions