Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to update an array inside an array? #2482

Unanswered
ghost asked this question in Q&A
Discussion options

I have a PurchaseRequest (like a Cart) and inside I have products...


{
 "id": "62862f04375eed096c6abc32",
 "products": [
 {
 "id": "628667dccee1aa7afe760d24",
 "currency": "MXN",
 "unit_cost": 10,
 "product_id": 1,
 "quantity": 1,
 "metadata": []
 }
 ]
}

I am not able to update just the position 0 of the array products.

I tried with:

$purchaseRequest->update(['products' => [$currentProductIndex => $currentProduct]]);

But this replaces the whole products.

I tried with:

$purchaseRequest->update(['products.' . $currentProductIndex => $currentProduct]);

But it doesn't do anything.

You must be logged in to vote

Replies: 1 comment

Comment options

I've solved it by debugging but I came across another bug...

PurchaseRequest::where(
 [
 ['_id', '=', new ObjectId($purchaseRequestId)],
 ['products.id', '=', $productId]
 ]
)->update(
 [
 '$set' => [
 "products.$" => $currentProduct,
 ]
 ]
);

I am using the timestamps for update, created and deleted and it is causing the following error, setting $set inside $set

src/Query/Builder.php

image

image

So I had to remove the $set inside my update statement, and leave only products.$

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2389 on December 17, 2022 18:07.

AltStyle によって変換されたページ (->オリジナル) /