schmittjoh / serializer

Library for (de-)serializing data of any complexity (supports JSON, and XML)
http://jmsyst.com/libs/serializer
MIT License
2.32k stars 586 forks source link

Use another field than "id" to find Doctrine entity #1533

Closed ebuildy closed 10 months ago

ebuildy commented 10 months ago
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no

Steps required to reproduce the problem

Given this JSON

[{"title":"toto"}]

Doctrine entity class:

class Hotel {
  private $title: string;
} 

When doing:

$this->serializer->deserialize("JSON", sprintf('array<%s>', Hotel::class), 'json');

Expected Result

Actual Result

This could call the entity repository as fallback to find the Entity ?

Something like

if (empty($identifierList)) {
            $object = $objectManager->getRepository($metadata->name)->findSpecial($data);
        } else {
            $object = $objectManager->find($metadata->name, $identifierList);
        }
scyzoryck commented 10 months ago

Hi! I do not fully understand a need for such feature. Looking at the Doctrine documentation:

Every entity class must have an identifier/primary key

On the other side - I can see many potential issues with implementing such possibility - how to configure which field/fields should be used, what if the field is not unique.

So if other people do not see a need for such feature I guess implementing your own fallback constructor would do the job in your case and I guess would be the simplest solution.

Best, Marcin.

ebuildy commented 10 months ago

what about an event serializer.entity_reconciliation ?

Or the entity repository could implements an interface reconciliateEntity that define a method reconciliateEntity(array $data)

scyzoryck commented 10 months ago

Sounds a bit complicated and redundant. :) I do not see a lot of value in adding it right now - especially when it can be achieved with existing features.