nilportugues / php-json-api

JSON API transformer outputting valid (PSR-7) API Responses.
http://nilportugues.com
MIT License
71 stars 35 forks source link

Q: doctrine usage #82

Open basz opened 7 years ago

basz commented 7 years ago

I see this library supports doctrine entities as input to the serialiser.

https://github.com/nilportugues/php-json-api/blob/aa305a4ea7a9aef0be3cf243c1e2cf3c9efb3411/tests/Integrations/Doctrine/DoctrineTest.php

That example works without relationships. It seems I get into an infinite loop when I have two entities referencing each other.

identity has many preferences.

So either I am doing something wrong, perhaps I should not fetch eager or something. Or this is not possible with this library or I might have found a bug...

https://gist.github.com/basz/898f6981ac12a6a43c3edc19d607a29c

I have pinned it down to the serialiser::serializeData method in the serializer.

basz commented 7 years ago

i see this is actually an issue with your serialiser https://github.com/nilportugues/php-serializer/issues/14

basz commented 7 years ago

Perhaps not : The JsonApiSerializer extends DeepCopySerializer which doesn't seem to protect against circular dependencies...

nilportugues commented 7 years ago

Circular dependencies are bad design.

I can code against bad design, but it really introduces lots of complexity to the library to support a bad practice.

basz commented 7 years ago

Hi, I rather do it right then wrong... But I would argue circular dependencies aren't bad design perse. They exist in doctrine entities. Think of a one to many relationship. Although I am never interested in traversing the identity from a Preference. I want the relationship defined in the owning side (preference) becuase of the constained advantage the cascade option gives me. (When ever I remove a user all his preferences are also deleted.)

ps. Simply changing the DeepCopySericalizer to Serializer fixed this...

<?xml version="1.0"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Identity">
    <id name="id" type="integer">
      <generator strategy="AUTO"/>
    </id>
    <field name="name" type="string" length="255" nullable="true"/>
    <one-to-many field="preference" target-entity="Preference" mapped-by="identity"/>
  </entity>
</doctrine-mapping>

<?xml version="1.0"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Preference">
    <id name="id" type="integer">
      <generator strategy="AUTO"/>
    </id>
    <field name="name" type="string" length="255" nullable="true"/>
    <many-to-one field="identity" target-entity="Identity" inversed-by="preference">
      <join-columns>
        <join-column name="identity_id" referenced-column-name="id" on-delete="CASCADE"/>
      </join-columns>
    </many-to-one>
  </entity>
</doctrine-mapping>
basz commented 7 years ago

thoughts? advise? still stuck at serialising entities