I've got an empty field called "accommodation" I try to append with an array of datas using this code:
r\table('xxx')->get($_POST['id'])->update(['accommodations' =>
row('accommodations')->append(['name' => $_POST['name'] , 'checkin' =>
$checkin , 'checkout' => $checkout , 'status' => 'rq'])])->run($conn);
But nothing is updated so far. I've checked about all my variables and all is fine (the ID, name, checkin and checkout dates...) and I don't have any php error as well. php rql api manual is not very specific about the way to append a nested field so I have no clue what I'm doing wrong there. Hope someone can help me on this one.
You say accomodations
field is empty: if it's not of type array (null
for instance), or if it doesn't exist, then the query will be rejected. You may use a branch
to test all cases:
r\table('xxx')->get($_POST['id'])->update(function($doc){
return [
'accomodations' => r\branch(
// test the field does not exist or is not an array
$doc->hasFields('accomodations')->not()->or($doc('accomodations')->typeOf()->ne('ARRAY')),
// test true: absent or not an array --> override the value
[
[
'name' => $_POST['name'],
'checkin' => $checkin ,
'checkout' => $checkout ,
'status' => 'rq'
]
],
// test false: exists as an array --> append
$doc('accomodations')->append([
'name' => $_POST['name'],
'checkin' => $checkin ,
'checkout' => $checkout ,
'status' => 'rq'
])
)
];
})->run($conn);
Not tested with PHP connector though. Hope this helps!