Skip to content

Problems using ResourceDocument::fromObject #76

Description

@ArminSchulz

Hi, we use your implementation quite a lot in our projets, and until now, never had any problems (we appreciate your work tremendously).
But now we have problems with answering a PUT request.
using your code leads to a structure were the attributes of an ResourceObject more or less look like this

"data": {
"type": "Event",
"id": "196",
"attributes":{
"type": "Event",
"id": "196",
"attributes": {
"ClientID": 1
}
}
}
which leads to an exception (type already used)

A solutions which works for us is simply:
rewriting the function in ResourceDocument in this way:

public static function fromObject($attributes, $type=null, $id=null, array $options=[]) {
$resourceDocument = new self();
$resourceDocument->setPrimaryResource($attributes->getResource(), $options);
return $resourceDocument;
}
the document includes now all links, relationships, and so on, from the ResourceObject

Activity

  1. lode commented on Jan 6, 2026

    @lode
    Owner

    Hey @ArminSchulz, sorry for the delay.

    Yeah. Currently the fromObject() the meant to be used in a way where the object is the bag containing only the attributes, e.g. class { public $ClientID = 1; }. You can read it as fromAttributesObject(), instead as fromResourceObject(). If you are already composing ResourceObjects yourself you can indeed use the setPrimaryResource() for that.

    Actually I was planning to deprecate the fromObject() methods as they are more confusing than helpful I think. But I could easily add a ResourceDocument::fromResourceObject() if that helps you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions