GDS\Gateway\RESTv1::upsert PHP Method

upsert() protected method

Put an array of Entities into the Datastore. Return any that need AutoIDs
protected upsert ( array $arr_entities ) : Entity[]
$arr_entities array
return GDS\Entity[]
    protected function upsert(array $arr_entities)
    {
        /* @var $arr_auto_id_required \GDS\Entity[] */
        $arr_auto_id_required = [];
        // Keep arrays of mutation types, so we can be more comfortable later with the ID mapping sequence
        $arr_inserts = [];
        $arr_upserts = [];
        foreach ($arr_entities as $obj_gds_entity) {
            // Build a REST object, apply current partition
            $obj_rest_entity = $this->createMapper()->mapToGoogle($obj_gds_entity);
            $this->applyPartition($obj_rest_entity->key);
            if (null === $obj_gds_entity->getKeyId() && null === $obj_gds_entity->getKeyName()) {
                $arr_inserts[] = (object) ['insert' => $obj_rest_entity];
                $arr_auto_id_required[] = $obj_gds_entity;
                // maintain reference to the array of requested auto-ids
            } else {
                $arr_upserts[] = (object) ['upsert' => $obj_rest_entity];
            }
        }
        // Build the base request, add the prepared mutations
        $obj_request = $this->buildCommitRequest();
        $obj_request->mutations = array_merge($arr_inserts, $arr_upserts);
        // Run
        $this->executePostRequest('commit', $obj_request);
        return $arr_auto_id_required;
    }