From eb88bd727863b3a750dafe782cf5e1a55e92ec82 Mon Sep 17 00:00:00 2001 From: Timothy Teoh Date: Thu, 19 Jul 2018 12:38:14 +0800 Subject: [PATCH] feat: Check whether a given model has the incrementing property and refer to insertId with the getKey() function to allow for use on models where the PK is not 'id'. Put in a check for the DB::connection to allow for use on multiple DBs. Check against the fields on the model (which toArray() will check against) --- src/Big.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Big.php b/src/Big.php index 1373f1f..0f9da2d 100644 --- a/src/Big.php +++ b/src/Big.php @@ -168,13 +168,13 @@ public function prepareData($data) // We loop our data and handle object conversion to an array foreach ($data as $item) { if (!is_array($item)) { - $item = $item->toArray(); + $item_arr = $item->toArray(); } $struct = []; // Handle nested array's as STRUCT<> - foreach ($item as $field => $value) { + foreach ($item_arr as $field => $value) { // Map array's to STRUCT name/type if (is_array($value)) { foreach ($value as $key => $attr) { @@ -186,16 +186,16 @@ public function prepareData($data) } } - // If we have an id column use Google's insertId + // If we have an incrementing column use Google's insertId // https://cloud.google.com/bigquery/streaming-data-into-bigquery#dataconsistency - if (array_key_exists('id', $item)) { + if ($item->incrementing) { $rowData = [ - 'insertId' => $item['id'], - 'data' => $item, + 'insertId' => $item->getKey(), + 'data' => $item_arr, 'fields' => $struct, ]; } else { - $rowData = ['data' => $item]; + $rowData = ['data' => $item_arr]; } // Set our struct definition if we have one @@ -288,9 +288,13 @@ public static function flipModel($model, $structs) // Cache our results as these rarely change $fields = Cache::remember($cacheName, $liveFor, function () use ($model) { - return DB::select('describe ' . $model->getTable()); + return DB::connection($model->getConnectionName())->select('describe ' . $model->getTable()); }); + //excludes fields that are hidden + $fields = collect($fields)->reject(function ($field) use ($model) { + return in_array($field->Field, $model->getHidden()); + })->all(); // Loop our fields and return a Google BigQuery field map array return ['fields' => static::fieldMap($fields, $structs)]; }