Describe the magic of object completition in endpoints

Issue #30 resolved
Martin Keller created an issue

It would be nice to see in the documentation, how objects are completed and how to enable/control this behavior. Maybe I have not yet done enough RTFM, then I apologize.

Background: debugging of a model in an endpoint shows minimal properties. After sending the results out, the resulting JSON is filled with more relevant information to the originalObject. Sometimes so much, that you need a distiller and dotted array notation to only get the relevant-to-you information.

Comments (4)

  1. David Bascom

    Hello Martin!
    Could you give me a more detailled example?

    In the most basic usecase with the default settings of EXT:nnrestapi, returning a Model will result in a recursive JSON containing ALL the fields / properties that can be accessed in the Model, e.g.

    public function getExampleAction() {
      $model = $this->myrepository->findByUid(1);
      return ['result'=>$model];
    }
    

    will return:

    {
      "result":
        "uid" => 1,
        "pid" => 2,
        "crdate" => ...,
        "tstamp" => ...,
        "title" => "...",
        "bodytext" => "...",
        // all other properties
    }
    

    This is the default and the intended structure. (Note: For recursive Objects, the resulting depth can be limited using an Annotation)

    The idea is, that if the frontend modifies the data and uses the identical keys, then EXT:nnrestapi can take care of merging and persisting the modified properties on-the-fly. Becaus the data is merged, note that the frontend doesn’t need to pass all keys back to the POST/PUT Endpoint:

    // JSON sent to the PUT/POST-Endpoint that persists the Model in the DB
    // e.g. postExampleAction( Example $model )
    {
      "uid" => 1,
      "title" => "New title"
    }
    

    All existing (persisted) properties of the Model are automatically merged using \nn\t3::Obj( $model )->merge( $arr ).
    We have an example here in the docs. Here is the syntax for the Obj->merge() helper method used.

    If you would like to reduce the data returned by the REST Api you can use a custom function or one of the distillers shipped with nnrestapi.

    Further reading:
    - Setting globalDistillers in TypoScript
    - Using the Distiller Annotation
    - Setting a max depth of the JSON for recursive Objects

  2. Martin Keller reporter

    Ok, I think the day was too long for me. Works as described!

    Now, with distillers new key=>dotted-array-notation, I have ambigous keys which leads to the wish of limiting the added models…
    My workaround: build the returned array myself with only needed data. This also saves extending the JSON with a lot of models and distilling them out afterwards.

  3. Log in to comment