I am trying to modify single field value, but whenever I do it it erases all other fields leaving updated field only, how can I modify a single field witouth affecting the other ones?
so far this is what I'm doing:
//
$updateResult2 = $coll_rutas->updateOne(
/* LIMIT TO ROUTE ID */
[
"_id" => new \MongoDB\BSON\ObjectId($ruta_id)
],
/* UPDATE VALUE */
[
'$set' => [
'destinos.$[d]' => [
"horas" => "1",
"minutos" => "15",
"millas" => "150",
"orden" => "1",
"notas" => "demo notes",
],
],
],
/* WHERE FILTER */
[
"upsert" => true,
'arrayFilters' => [
['d.ciudad_id' => 47]
]
]
);
//var_dump($updateResult2); exit;
This is the original mongo Object:
"destinos": [
{
"ciudad_id": "47",
"nombre_destino": "Salt Lake City, Utah",
"horas": "0",
"minutos": "0",
"millas": "0",
"orden": 1,
"notas": null,
"origenes": [
{
"ciudad_id": "45",
"nombre_origen": "Provo, Utah",
"orden": 1,
"notas": null,
"base_price": 0
}
]
}
],
With my changes it erases origenes array, ciudad_id and nombre destino, leaving it this way:
"destinos": [
{
"horas": "1",
"minutos": "15",
"millas": "150",
"orden": "1",
"notas": "demo notes"
]
}
],
With this code:
'$set' => [
'destinos.$[d]' => [
"horas" => "1",
"minutos" => "15",
"millas" => "150",
"orden" => "1",
"notas" => "demo notes",
],
]
you are targeting the destinos.$[d] element and replacing all of its contents with the new data structure.
You need to use the .(dot) notation to target each element in the nested array. It's a bit verbose but this would work:
'$set' => [
'destinos.$[d].horas' => "1",
'destinos.$[d].minutos' => "15",
'destinos.$[d].millas' => "150",
'destinos.$[d].orden' => "1",
'destinos.$[d].notas' => "demo notes"
]
Related
I have created an array to convert into json file in php. But as per the requirement I need to assign an array to this array element. it's an array inside an array.
"data" => array( // data array
array(
"event_name" => "Purchase",
"event_time" => time(),
"event_id" => $order_id,
"user_data" => array(
"client_ip_address" => $ip,
"client_user_agent" => $browser,
"em" => $email,
"ph" => $phone,
"fbc" =>$fbp,
"fbp" =>$fbc,
"fn" => $fn,
"ct" => $ct,
"st" => $st,
"zp" => $zp,
"country" => $country,
"external_id" => $email
),
"contents" => $items,
// "id" => $item_ids,
// "quantity" => count($item_qty),
// // "delivery_category"=> "home_delivery",
// "order_id" => $order_id
// ),
"custom_data" => array(
"currency" => "GBP",
"value" => $order_total,
),
"action_source" => "website",
"event_source_url" => $fullURL,
),
)
);
But my $item array look like this when I echo
{
"id": 814,
"order_id": 36956,
"name": "Pearson BTEC Level 7 Certificate in Strategic Management and Leadership (RQF)",
"product_id": 19555,
"variation_id": 0,
"quantity": 2,
"tax_class": "",
"subtotal": "1158",
"subtotal_tax": "0",
"total": "1158",
"total_tax": "0",
"taxes": {
"total": [],
"subtotal": []
},
"meta_data": []
},
{
"id": 815,
"order_id": 36956,
"name": "Pearson BTEC Level 7 Diploma in Strategic Management and Leadership (RQF)",
"product_id": 19107,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"subtotal": "999",
"subtotal_tax": "0",
"total": "999",
"total_tax": "0",
"taxes": {
"total": [],
"subtotal": []
},
"meta_data": []
}
And when I assign this $items and converted to json then I can see only the ides of the array
"user_data": {
"client_ip_address": "2402:d000:a200:a4a8:f06d:40a:cf85:e26d",
"client_user_agent": "",
"em": "b99526fedef7fe1da29e27d6f09cb64efew536978778e6cde3d8277d71a04398d1a4",
"ph": "db80005a39b11372054b7708cac76gsaaww47af6ed3fe4b37b9a8b2be435e2fcff04a7",
"fbc": "",
"fbp": "",
"fn": "83cd072a16ddb3d793c2f5bcd7d0basdree5a04af680279cf6a26fb8dffe57ce3b51a",
"ct": "17a0730b80e55f4bee9569be3f7e6d0wqw500c5568ecb8d785759d7151ef40f5bbd",
"st": "e3b0c44298fc1c149afbf4c8996fb9sas2427ae41e4649b934ca495991b7852b855",
"zp": "ac7bb74de6884ffce919c15fef4193749sdsd4588026c643973946713ac46da540d",
"country": "c820736841e811c96d30b14eb998feb6bsddc2c9d0764b7731267438dc7d6bb5b59",
"external_id": "b99526fedef7fe1da29e27d6f09ssscb64536978778e6cde3d8277d71a04398d1a4"
},
"contents": [
{
"814": {},
"815": {}
}
],
result should be look like this in this way in "centents" element
"contents" : [{'id':'ABC123','quantity' :2,'item_price':5.99}, {'id':'XYZ789','quantity':2, 'item_price':9.99, 'delivery_category': 'in_store'}]
I think the problem is that your $items, is actually an array of "strings" (json)
try to do a json_decode() on those $items before adding it to the content.
anyway, when you ask a question try to remove all unnecessary stuff, and make an example with the minimum code you need help with, in this case it takes a lot of reading through stuff just to understand the issue.
I'm trying to validate the following json file but I can't find the way to access the "Address" child, how should I do it? Everything goes fine until it tries to access the "address" field.
Json:
{
"first_name": "Test",
"last_name": "Test",
"email": "test#gmail.com",
"mobile_phone": 123456789,
"address": {
"country": "Test",
"postal_code": "Test",
"city": "Test",
"street": "Test",
"number": "Test",
"floor": "Test",
"door": "Test"
},
"password": "Test",
"birth_date": "2005-12-30"
}
Code:
$data = $this->validator->validate($request, [
"first_name" => v::stringVal()->notEmpty(),
"last_name" => v::stringVal()->notEmpty(),
"email" => v::notEmpty()->email(),
"mobile_phone" => v::intVal()->notEmpty(),
"address" =>[
"country" => v::stringVal()->notEmpty(),
"postal_code" => v::stringVal()->notEmpty(),
"city" => v::stringVal()->notEmpty(),
"street" => v::stringVal()->notEmpty(),
"number" => v::stringVal()->notEmpty(),
"floor" => v::stringVal()->notEmpty(),
"door" => v::stringVal()->notEmpty(),
],
"password" => v::base64()->notEmpty(),
"birth_date" => v::date()->notEmpty()
]);
Looks like you are using Respect\Validation\Validator. We can make use of chained validation and the Key function to validate sub-arrays as shown in the docs.
$data = [
'parentKey' => [
'field1' => 'value1',
'field2' => 'value2'
'field3' => true,
]
];
Using the next combination of rules, we can validate child keys.
v::key(
'parentKey',
v::key('field1', v::stringType())
->key('field2', v::stringType())
->key('field3', v::boolType())
)
->assert($data); // You can also use check() or validate()
Applied to your data, the validation could look like this:
use Respect\Validation\Validator as v;
$json = json_decode('{
"first_name": "Test",
"last_name": "Test",
"email": "test#gmail.com",
"mobile_phone": 123456789,
"address": {
"country": "Test",
"postal_code": "Test",
"city": "Test",
"street": "Test",
"number": "Test",
"floor": "Test",
"door": "Test"
},
"password": "Test",
"birth_date": "2005-12-30"
}', true);
v::create()
->key('first_name', v::stringType()->length(1, 32))
->key('last_name', v::stringVal()->notEmpty())
//->..
->key('address',
v::key('country', v::stringType()->length(1, 32))
->key('postal_code', v::stringType()->length(1, 32))
->key('city', v::stringType()->length(1, 32))
//->..
)
->key('password', v::stringVal()->notEmpty())
->key('birth_date', v::stringVal()->notEmpty())
->assert($json); // You can also use check() or validate()
That is the way to access the child of the json:
$this->validator->validate($request, [
"first_name" => v::stringVal()->notEmpty(),
"last_name" => v::stringVal()->notEmpty(),
"email" => v::notEmpty()->email(),
"mobile_phone" => v::intVal()->notEmpty(),
"address" => v::notEmpty()
->key("country", v::stringVal()->notEmpty())->key("postal_code", v::stringVal())->key("city", v::stringVal()->notEmpty())->key("street", v::stringVal()->notEmpty())->key("number", v::stringVal())->key("floor", v::stringVal())->key("door", v::stringVal()),
"password" => v::base64()->notEmpty(),
"birth_date" => v::date('d/m/Y')->notEmpty()
]);
I am using laravel 5.5 API resource collection for showing list of products. I am using an array instead of the modal object. I got my list of items but i am not sure how to show item variant array in the response. The actual response array passing to the api resource are:
{
"ID": 3160,
"UserID": 3302,
"ItemName": "2",
"Description": "2",
"Price": 2,
"StockLimited": true,
"StockQuantity": 19,
"CurrencyCode": "USD",
"ImageUrl": "/images/items/Item3302-636434761494584365-qq5HFm.png",
"VariantItems": [
{
"ID": 3181,
"ItemName": "2",
"Price": 0,
"StockLimited": false,
"StockQuantity": 0,
"CurrencyCode": "USD",
"Variants": [
{
"Id": 1099,
"Name": "Red",
"VariantGroupId": 1027,
"VariantGroupName": "Colour"
},
{
"Id": 1111,
"Name": "S",
"VariantGroupId": 1028,
"VariantGroupName": "Size"
}
]
},
{
"ID": 3182,
"ItemName": "2",
"Price": 0,
"StockLimited": false,
"StockQuantity": 0,
"CurrencyCode": "USD",
"Variants": [
{
"Id": 1099,
"Name": "Red",
"VariantGroupId": 1027,
"VariantGroupName": "Colour"
},
{
"Id": 1112,
"Name": "M",
"VariantGroupId": 1028,
"VariantGroupName": "Size"
}
]
}
],
"MerchantName": "seller",
"VariantGroupLists": [
{
"VariantGroupName": "Colour",
"Names": [
"Red",
"Grey",
"Navy"
]
},
{
"VariantGroupName": "Size",
"Names": [
"S",
"M",
"L",
"XL"
]
}
]
}
My ItemResourceCollection
public function toArray($request)
{
return [
'data' => $this->collection->map(function ($item) use ($request) {
return (new ItemResource($item))->toArray($request);
})
];
}
My ItemResource
public function toArray($request)
{
return [
"item_id" => $this->resource->ID,
"item_name" => $this->resource->ItemName,
"description" =>$this->resource->Description,
"item_price"=>$this->resource->Price,
"stock_qty"=>$this->resource->StockQuantity,
"currency_code"=>$this->resource->CurrencyCode,
"item_image_url"=>$this->resource->ImageUrl,
];
}
public function with($request)
{
return ['item_varients' => [new ItemResource($this->resource->VariantItems)]];
}
What I am trying to achieve is this result:
[
{
"item_id": 3160,
"item_name": "BLACK COWL NECK DRESS WITH DIAMANTE DETAIL",
"description": "Test test",
"item_price": 16.99,
"stock_qty": 20,
"currency_code": "SGD",
"item_image_url": "/images/items/Item18-636231488325192562-GoiIBl.png",
"item_varients": [
{
"id": 29,
"name": "Red",
"varientgroupId": 11,
"varientgroupname": "Color"
}
]
}
]
the with() method in ItemResource is not working. How do I add the "item_varients" array in the resource response?
I would first move the line to add the item_varients property inside the method toArray like following:
public function toArray($request)
{
return [
"item_id" => $this->resource->ID,
"item_name" => $this->resource->ItemName,
"description" => $this->resource->Description,
"item_price" => $this->resource->Price,
"stock_qty" => $this->resource->StockQuantity,
"currency_code" => $this->resource->CurrencyCode,
"item_image_url" => $this->resource->ImageUrl,
"item_varients" => [
new ItemResource($this->resource->VariantItems)
]
]
}
Meta information
We do that because the with is meant for to add a meta information block or something similar. See https://laravel.com/docs/5.5/eloquent-resources#adding-meta-data
The advantage when using the with method is, that multiple resources results in only one meta block. This is not what you want, I think.
Resource for VariantItem
I think you have to build a Resource class also for VariantItem. And because the VariantItems are a collection you also have to fill your array called item_variants similar to your ItemResourceCollection. So change your code to look like this:
public function toArray($request)
{
/**
* Prepare your variant items to have a collection
*
* #var Collection $variantItems
*/
$variantItems = collect($this->resource->VariantItems);
return [
"item_id" => $this->resource->ID,
"item_name" => $this->resource->ItemName,
"description" => $this->resource->Description,
"item_price" => $this->resource->Price,
"stock_qty" > $this->resource->StockQuantity,
"currency_code" => $this->resource->CurrencyCode,
"item_image_url" => $this->resource->ImageUrl,
"item_varients" => VariantItem::collection($variantItems)
]
}
Here is the best way to get specific fields using a collection
return [
'id' => $this->id,
'variant_item' => new CustomResource($this->user, ['item_id', 'item_name', 'item_size'])
];
I'm working with Elasticsearch-PHP client. I want to index my datas, but I have a problem with mapping. There is a problem: I create my data array, everything works fine, but when I add this array to my index body => my_data_array some datas show up, but not all of them. I don't know why. I just digged and try all of following steps but nothing changed.
I just attached my snippets.
This my controller file where I index datas:
{
$params = ['body' => []];
foreach($all_ads as $key => $ads){
$params['body'][] = [
'index' => [
'_index' => 'demo_data',
'_type' => 'demo',
'_id' => $ads->id
]
];
$params['body'][] = $ads->indexParams();
}
$responses = $client->bulk($params);
this is result json object:
"response": [
{
"id": 85345,
"old_id": "5088063",
"user_id": "2706",
"category_id": "15",
"type": "3",
"title": array[3],
"slug": "",
"sub_region_id": "8",
"condition": "1",
"username": "John Doe",
"price": "82000",
"price_type": "1",
"no_phone": "0",
"views": "29",
"hot": "0",
"vip": "0",
"price_measure": "0"
}
I have a datafield and it is not visible here.
Its data mapping structure
'data' => [
'type' => 'object',
'properties' => [
'key_id' => ['type' => 'integer'],
'value_id' => ['type' => 'integer'],
'key' => ['type' => 'keyword'],
'value' => ['type' => 'keyword']
]
]
How can I fix it?
I just check some question from here and github issue forum but nothing helps. Thanks!
You have to follow solution of the question. Its maybe helpful for you.
Elasticsearch mapping not working as expected
Ask Question
I have two arrays that are the results of two different database queries. I want to merge those two arrays fin the way I explain in the example below
This is the code that executes the first query:
$query = $this->db->query("SELECT kec,pilihan,COUNT(pilihan) as total FROM votes GROUP BY pilihan");
$someArray = [];
foreach($query->result_array() as $row){
array_push($someArray, [
'data' => $row['pilihan'],
'total' => $row['total'],
'location' => $row['kec'],
]);
}
This is the resulting array:
[
{
"data": "1",
"total": "2",
"location": "3524230"
},
{
"data": "2",
"total": "3",
"location": "3524230"
},
{
"data": "3",
"total": "1",
"location": "3524230"
}
]
This is the code that executes the second query:
$query = $this->db->query("SELECT * FROM owl_dptmaster GROUP BY kecamatan");
$someArray = [];
foreach($query->result_array() as $row){
array_push($someArray, [
'location' => $row['kecamatan']
]);
}
And the second array looks like this:
[
{
"location": "3524230"
},
{
"location": "3524240"
},
{
"location": "3524250"
},
{
"location": "3524260"
}
]
I want to merge those two arrays so that the result would be this array:
[
{
"location": "3524230",
"data1": "2",
"data2": "3",
"data3": "1"
},
{
"location": "3524240",
"data1": null,
"data2": null,
"data3": null
},
{
"location": "3524250",
"data1": null,
"data2": null,
"data3": null
},
{
"location": "3524260",
"data1": null,
"data2": null,
"data3": null
}
]
How can I achieve this?
Instead of combining arrays, you could do the summarizing in sql with sub queries. Assuming you're using mysql.
$query = $this->db->query(
"select owl.kecamatan,
(select count(pilihan) from votes where kec = owl.kecamatan and pilihan = 1) data1,
(select count(pilihan) from votes where kec = owl.kecamatan and pilihan = 2) data2,
(select count(pilihan) from votes where kec = owl.kecamatan and pilihan = 3) data3,
from owl_dptmaster owl
order by owl.kecamatan"
);
You could do this query and then build an array of "data" . $pilihan => $totalPilihan which you push into the $someArray when the $location changes.
$query = $this->db->query(
"select owl.kecamatan as location, v.pilihan
from owl_dptmaster owl
left join votes v
on owl.kecamatan = v.kec"
order by 1, 2);
You have a results set with all the locations and votes.
// Made of query results...
$query = [
[ "location" => "3524230", "pilihan" => "1" ],
[ "location" => "3524230", "pilihan" => "1" ],
[ "location" => "3524230", "pilihan" => "2" ],
[ "location" => "3524230", "pilihan" => "3" ],
[ "location" => "3524230", "pilihan" => "3" ],
[ "location" => "3524230", "pilihan" => "3" ],
[ "location" => "3524230", "pilihan" => "4" ],
[ "location" => "3524230", "pilihan" => "4" ],
[ "location" => "3524230", "pilihan" => "5" ],
[ "location" => "3524240", "pilihan" => null ],
[ "location" => "3524250", "pilihan" => null ],
[ "location" => "3524260", "pilihan" => null ],
[ "location" => "3524270", "pilihan" => "1" ],
[ "location" => "3524270", "pilihan" => "1" ],
[ "location" => "3524270", "pilihan" => "1" ],
[ "location" => "3524270", "pilihan" => "20" ],
[ "location" => "3524270", "pilihan" => "20" ],
[ "location" => "3524270", "pilihan" => "30" ]
];
And here's a procedural routine to build $someArray[].
$someArray = [];
$xRow = [];
$oldPilihan = '';
$total = 0;
$oldLocation = '';
foreach ($query as $row) {
//echo $row['location'] . " - " . $row['pilihan'] . '<br>';
$location = $row['location'];
$pilihan = $row['pilihan'];
if ($pilihan !== $oldPilihan) {
if (! empty($oldPilihan) ) {
$dataName = 'data' . $oldPilihan;
$xRow[] = array($dataName => $total);
}
$total = 1;
$oldPilihan = $pilihan;
} else {
$total++;
}
if ($location !== $oldLocation) {
if (! empty($oldLocation)) {
array_push($someArray, $xRow);
}
$xRow = [ 'location' => $location ];
$oldLocation = $location;
}
}
$dataName = 'data' . $oldPilihan;
$xRow[] = array($dataName => $total);
array_push($someArray, $xRow);
var_dump($someArray);