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

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

Open
robotccm opened this issue Sep 10, 2019 · 0 comments
Labels
3.5.0 bug Something isn't working major

Comments

@robotccm
Copy link

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
@robotccm robotccm added major bug Something isn't working 3.5.0 labels Oct 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.5.0 bug Something isn't working major
Projects
None yet
Development

No branches or pull requests

1 participant