serializer icon indicating copy to clipboard operation
serializer copied to clipboard

DoctrineObjectConstructor Error undefined index id

Open ngocongcan opened this issue 6 years ago • 2 comments

Hi JMS,

I got issue when deserialize JSON, these lines of code throw an exception `foreach ($classMetadata->getIdentifierFieldNames() as $name) { if ($visitor instanceof AbstractVisitor) { /** @var PropertyNamingStrategyInterface $namingStrategy */ $namingStrategy = $visitor->getNamingStrategy(); $dataName = $namingStrategy->translateName($metadata->propertyMetadata[$name]); } else { $dataName = $name; }

        if (!array_key_exists($dataName, $data)) {
            return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
        }

        $identifierList[$name] = $data[$dataName];
    }`

Since I don't add the annotation before the identify field:

  • @JMS\Expose
  • @JMS\Groups({"inspection-report", "audit-report"})

So, Do we have to add it before identify fields?

Thanks!

ngocongcan avatar Aug 16 '18 02:08 ngocongcan

I have the same issue and I don't think this invalid.

It will happen when we have a entity that does't expose it's primary key. (For example a entity that can only be created but never updated or get).

Just a extra if would do the job since those line that are already present will take care of the rest:

if (!array_key_exists($dataName, $data)) { return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context); }

mpoiriert avatar Oct 28 '18 21:10 mpoiriert

I am facing the same issue. @mpoiriert @ngocongcan Did you guys find a workaround?

JodyLognoul avatar Dec 20 '18 09:12 JodyLognoul