Skip to content

Add support for custom PK names, hidden attributes, and different DB connections #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/Big.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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)];
}
Expand Down