orm icon indicating copy to clipboard operation
orm copied to clipboard

Missing type casting call for primary key

Open roquie opened this issue 2 years ago • 2 comments

PHP Version: 8.0 Cycle ORM: 1.5.0

If you use the UUID as the primary key, then when fetching related records, the ORM crashes with an error:

[TypeError]
array_key_exists(): Argument #1 ($key) must be a valid offset type.

https://github.com/cycle/orm/blob/master/src/Parser/AbstractNode.php#L341

How to reproduce:

  1. Follow this guide https://cycle-orm.dev/docs/advanced-column-wrappers
  2. Follow this guide https://cycle-orm.dev/docs/advanced-uuid (but don't forget to replace Ramsey UUID with a typecasted object from p.1 in nextPrimaryKey).

Possible fix:

    protected function mountArray(string $container, string $key, $criteria, array &$data): void
    {
        $criteria = (string) $criteria; // fix

        if (!array_key_exists($criteria, $this->references[$key])) {
            throw new ParserException("Undefined reference `{$key}`.`{$criteria}`");
        }

        foreach ($this->references[$key][$criteria] as &$subset) {
            if (!in_array($data, $subset[$container], true)) {
                $subset[$container][] = &$data;
            }

            unset($subset);
            continue;
        }
    }

roquie avatar Aug 06 '21 07:08 roquie

It is correct fix. Would you like to create a PR?

roxblnfk avatar Aug 06 '21 08:08 roxblnfk

PR merged but still neet to add test case

roxblnfk avatar Aug 06 '21 11:08 roxblnfk

Outdated

roxblnfk avatar Sep 10 '23 15:09 roxblnfk