Append entry in array via API

hi,

I want to update an array (requests) within a collection (see below). Now I fetch all entries and push the new entry into a new array within the current entries and update the collection. But this could lead to a memory issue with many entries. Is there a way to append a new entry without fetching all previous entries with the curl api?

Here is the model structure:

{
  "client": "morituro",
  "pass": "3SWNtSL1f3fa8T6h9",
  "code": "1234",
  "requests": [],
  "_state": 1,
  "_modified": 1702979802,
  "_mby": null,
  "_created": 1698071066,
  "_cby": "a2ad0a06663236ab620000f6",
  "_id": "a13a28b3333161b99700034b",
}

Current solution:

$newEntry = array(
	"crdate" => time(),
	"request" => $_POST
);
array_push($requests, $newEntry);
$requests = json_encode($requests);

$req = '{
	"data": {
	   "requests": '.$requests.',
	   "_id": "'.$id.'"
	}
}';

anybody can help, please?

Suppose that your current model is ‘clients’.

Case 1:
Are you trying to fetch a single entry of the ‘clients’ model and append data to the ‘requests’ array?

Case 2:
Or are you trying to fetch all entries of the ‘clients’ model, loop through them all, and append data to the ‘requests’ array?

IMO, if you are in Case 1, it is okay to do so.

However, if you are in Case 2, it can result in a memory leak. In this case, you should separate the ‘requests’ property as a new model and link it to clients model using the ‘contentLink’ field.

By doing this, you can directly create a new item in the ‘requests’ model.

I assume that you are using Cockpit v2.

hi @ronaldaug,

thank you for helping me! its case 2. And yes, I’m using cockpit v2.

Ok, so I will save the requests to a new collection. But with “contentLink” i have to select the items manually. Is there a way to show them automatically?

Sorry for late reply,
Let’s consider the scenario where you are in case 2,

Option 1 - Update data through API

You need to trigger the API twice:

  1. Create a new item in the requests model (if it’s a one-to-many relationship, create multiple items).
  2. Retrieve the request IDs from step 1 and update the Clients model.
  "client": "morituro",
  "pass": "3SWNtSL1f3fa8T6h9",
  "code": "1234",
  "requests": [ <=== update this field
            {
                "_model": "requests",
                "_id": "5f63e04d3630645f3b000154"
            },
            {
                "_model": "requests",
                "_id": "5f91c0c03835300d7c00032c"
            }
        ],
  "_state": 1,
  "_modified": 1702979802,
  "_mby": null,
  "_created": 1698071066,
  "_cby": "a2ad0a06663236ab620000f6",
  "_id": "a13a28b3333161b99700034b",

Option 2 - Update data through Cockpit dashboard

In this case, you have no choice, you have to link it manually.


Note: You can also link both models.

e.g - Create a new property in requests model called client.
Suppose this is request fields

{
  'request_field_1':'value',
  'request_field_2':'value',
  'client': [
         "_model": "client",
         "_id": "5f91c0c03835300d7c00032c"
  ]
}

Then, you can filter out the object by using the client _id.