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

Q: doctrine usage #82

Open
basz opened this issue Sep 26, 2016 · 5 comments
Open

Q: doctrine usage #82

basz opened this issue Sep 26, 2016 · 5 comments

Comments

@basz
Copy link
Contributor

basz commented Sep 26, 2016

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
Copy link
Contributor Author

basz commented Sep 26, 2016

i see this is actually an issue with your serialiser nilportugues/php-serializer#14

@basz basz closed this as completed Sep 26, 2016
@basz
Copy link
Contributor Author

basz commented Sep 26, 2016

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

@basz basz reopened this Sep 26, 2016
@nilportugues
Copy link
Owner

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
Copy link
Contributor Author

basz commented Sep 26, 2016

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
Copy link
Contributor Author

basz commented Oct 24, 2016

thoughts? advise? still stuck at serialising entities

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants