From 32dcf31441cd1712857afc8ebcb7e9c8e4384759 Mon Sep 17 00:00:00 2001 From: Kath Young Date: Wed, 6 Sep 2023 11:19:30 +0930 Subject: [PATCH 1/2] Make the model agnostic of the driver, and parse objects appropriately --- src/Eloquent/Connection.php | 9 +++++++++ src/Eloquent/ZohoModel.php | 19 ------------------- src/Providers/ZohoServiceProvider.php | 11 ++++------- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/Eloquent/Connection.php b/src/Eloquent/Connection.php index 5fd26b9..f6bad03 100644 --- a/src/Eloquent/Connection.php +++ b/src/Eloquent/Connection.php @@ -134,6 +134,15 @@ public function zohoUpsert(string $toTable, array $data, array|string $key): int $key = [$key]; } + foreach ($data as $rowIndex => $row) { + foreach ($row as $field => $value) { + if (is_object($value) && method_exists($value, '__toString')) { + $row[$field] = $value->__toString(); + } + } + $data[$rowIndex] = $row; + } + return $this->getClient()->importUpsert($toTable, $data, $key); } diff --git a/src/Eloquent/ZohoModel.php b/src/Eloquent/ZohoModel.php index 7efecb4..718c7f1 100644 --- a/src/Eloquent/ZohoModel.php +++ b/src/Eloquent/ZohoModel.php @@ -8,7 +8,6 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Date; use Portable\EloquentZoho\Casts\ZohoInteger; -use Portable\EloquentZoho\Eloquent\Query\Builder as QueryBuilder; abstract class ZohoModel extends Model { @@ -26,24 +25,6 @@ public function __construct(array $attributes = []) $this->casts = array_merge($this->casts, ['id' => ZohoInteger::class]); } - /** - * {@inheritdoc} - */ - public function newEloquentBuilder($query) - { - return new Builder($query); - } - - /** - * {@inheritdoc} - */ - protected function newBaseQueryBuilder() - { - $connection = $this->getConnection(); - - return new QueryBuilder($connection, $connection->getQueryGrammar(), $connection->getPostProcessor()); - } - /** * Get the current connection name for the model. * diff --git a/src/Providers/ZohoServiceProvider.php b/src/Providers/ZohoServiceProvider.php index f32a840..42a2e81 100644 --- a/src/Providers/ZohoServiceProvider.php +++ b/src/Providers/ZohoServiceProvider.php @@ -3,7 +3,8 @@ namespace Portable\EloquentZoho\Providers; use Illuminate\Support\ServiceProvider; -use Portable\EloquentZoho\Eloquent\Connection; +use Portable\EloquentZoho\Eloquent\Connection as ZohoConnection; +use Illuminate\Database\Connection; class ZohoServiceProvider extends ServiceProvider { @@ -17,12 +18,8 @@ public function register(): void 'eloquent-zoho' ); // Add database driver. - $this->app->resolving('db', function ($db) { - $db->extend('zoho', function ($config, $name) { - $config['name'] = $name; - - return new Connection($config); - }); + Connection::resolverFor('zoho', function ($connection, $database, $prefix, $config) { + return new ZohoConnection($config); }); } From a7edaf865cd5851bbc2262b4d71c099e104378d8 Mon Sep 17 00:00:00 2001 From: Kath Young Date: Wed, 6 Sep 2023 12:45:43 +0930 Subject: [PATCH 2/2] Brevity fix per comments --- src/Eloquent/Connection.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Eloquent/Connection.php b/src/Eloquent/Connection.php index f6bad03..a9f6a9f 100644 --- a/src/Eloquent/Connection.php +++ b/src/Eloquent/Connection.php @@ -136,9 +136,7 @@ public function zohoUpsert(string $toTable, array $data, array|string $key): int foreach ($data as $rowIndex => $row) { foreach ($row as $field => $value) { - if (is_object($value) && method_exists($value, '__toString')) { - $row[$field] = $value->__toString(); - } + $row[$field] = $value instanceof \Stringable ? (string) $value : $value; } $data[$rowIndex] = $row; }