/** * @return \Google_Service_Datastore_Mutation * @throws MissingFieldsException */ public function build() { if (empty($this->upsert) && empty($this->update) && empty($this->insert) && empty($this->insertAutoId) && empty($this->delete)) { throw new MissingFieldsException(self::class, ['$upsert', '$update', '$insert', '$insertAutoId', '$delete']); } $mutation = new \Google_Service_Datastore_Mutation(); if (!empty($this->upsert)) { $mutation->setUpsert($this->upsert); } if (!empty($this->update)) { $mutation->setUpdate($this->update); } if (!empty($this->insert)) { $mutation->setInsert($this->insert); } if (!empty($this->insertAutoId)) { $mutation->setInsertAutoId($this->insertAutoId); } if (!empty($this->delete)) { $mutation->setDelete($this->delete); } $mutation->setForce($this->force); return $mutation; }
/** * Delete one or more entities based on their Key * * Consumes Schema * * @todo determine success? * * @param array $arr_entities * @return bool */ public function deleteMulti(array $arr_entities) { $obj_mutation = new \Google_Service_Datastore_Mutation(); $arr_google_keys = $this->createMapper()->createKeys($arr_entities); foreach ($arr_google_keys as $obj_key) { $this->applyNamespace($obj_key); } $obj_mutation->setDelete($arr_google_keys); $this->commitMutation($obj_mutation); $this->obj_schema = null; return TRUE; // really? }
public static function batchTxnMutate($txn, $batchput, $deletes = []) { if (!$txn) { throw new UnexpectedValueException('Transaction value not set.'); } $insert_auto_id_items = []; $upsert_items = []; $delete_items = []; foreach ($batchput as $item) { $entity = $item->create_entity(); if ($item->key_id || $item->key_name) { $upsert_items[] = $entity; } else { $insert_auto_id_items[] = $entity; } } foreach ($deletes as $delitem) { $delitem->beforeItemDelete(); $delete_items[] = self::createKeyForItem($delitem); } $mutation = new Google_Service_Datastore_Mutation(); if (!empty($insert_auto_id_items)) { $mutation->setInsertAutoId($insert_auto_id_items); } if (!empty($upsert_items)) { $mutation->setUpsert($upsert_items); } if (!empty($delete_items)) { $mutation->setDelete($delete_items); } $req = new Google_Service_Datastore_CommitRequest(); $req->setMutation($mutation); $req->setTransaction($txn); // will throw Google_Service_Exception if there is contention DatastoreService::getInstance()->commit($req); // successful commit. Call the onItemWrite method on each of the batch put items foreach ($batchput as $item) { $item->onItemWrite(); } }