Skip to content

Commit 6d4a4fd

Browse files
committed
added support to return nulls instead of empty strings
1 parent ca8903d commit 6d4a4fd

File tree

6 files changed

+41
-21
lines changed

6 files changed

+41
-21
lines changed

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ We support the [currently supported versions of Laravel](https://laravel.com/doc
2929

3030
## What's new in 2.0
3131
* Added support for Laravel 11
32+
* Empty fields in FileMaker are returned as null instead of an empty string
3233
* FileMaker Sessions only last for the duration of a single request to Laravel instead of being reused for 15 minutes - Cache is no longer required
3334
* Improvements to whereNot logic and implementation to make it behave more closely to what it should be
3435

@@ -37,28 +38,23 @@ Run `composer require gearbox-solutions/eloquent-filemaker:^2.0` to upgrade to t
3738

3839
#### Potential changes to your code
3940

40-
The upgrade to 2.0 should be pretty seamless for most use cases, but there are two minor changes to be aware of which could affect uncommon use cases. These cases are described below.
41+
The usage of the package has generally remained the same. However, there are a few changes which may affect your code. Read the changes below to see what refactoring may be necessary when upgrading.
4142

43+
##### Major - Changes to empty fields
44+
In version 1.0, empty fields in FileMaker were returned as an empty string. In version 2.0, empty fields are returned as null. This change makes working with FileMaker data a bit more like what a Laravel developer would expect from a database.
4245

43-
##### Changes to session management
46+
If you'd like to continue with the old behavior your can change the `empty_strings_to_null` config value to false to keep with the empty strings. Otherwise, if you have any code which relies on empty fields being returned as an empty string, you may need to refactor your code to work with the new behavior.
47+
48+
##### Minor - Changes to session management - Minor Change
4449
In version 1.0 the same FileMaker Data API session was used for all requests for about 15 minutes at a time, until the token expired. This token was stored in the Laravel Cache between requests. This behavior has been changed in version 2.0.
4550

4651
In this version 2.0, a Data API session lasts for only the duration of one request to your Laravel app. Login is performed the first time you use request data from the Data API. The session is ended and a logout is performed after the response has been sent to the browser through the use of [terminable middleware](https://laravel.com/docs/11.x/middleware#terminable-middleware).
4752

4853
If you have any code which relies on the Data API session being reused between requests to your Laravel app, such as setting a global field once and then reading it across multiple page loads of your Laravel app, you will need to refactor your code to work with the new behavior.
4954

50-
51-
##### Improvements to whereNot logic
55+
##### Minor - Improvements to whereNot logic
5256
There were some cases where whereNot may return results that were probably not correct or expected. This has been fixed in version 2.0. If you have any code which relies on the old, incorrect behavior of whereNot, you may need to refactor your code to work with the new corrected behavior.
5357

54-
55-
56-
## Requirements
57-
Laravel 10.0+
58-
59-
For Laravel versions greater than 7.3 and less than 9.0, use version [0.2.10](https://github.com/gearbox-solutions/eloquent-filemaker/blob/0.2.10)
60-
For Laravel 9 use version [1.](https://github.com/gearbox-solutions/eloquent-filemaker/tree/1.x)
61-
6258
# Installation
6359
Install `gearbox-solutions/eloquent-filemaker` in your project using Composer.
6460

@@ -99,6 +95,7 @@ As an example, let's say you have three tables - Organizations, Contacts, and In
9995
Creating model classes is the easiest way to access your FileMaker data, and is the most Laravel-like way of doing things. Create a new model class and change the extension class from `Model` to `FMModel`. This class change enables you to use the features of this package with your models.
10096

10197

98+
10299
#### Things that work
103100

104101
The FMModel class extends the base Laravel Model class, and can be used very similarly. It supports many standard Eloquent query builder features for working with data, such as where(), find(), id(), orderBy(), delete(), save(), and many more!
@@ -119,6 +116,13 @@ Your queries against your FileMaker database require you to get data from a part
119116
protected $layout = 'MyLayout';
120117
```
121118

119+
### Null values and empty strings
120+
Null is an important, expected possible value for developers when working with databases. FileMaker as a platform, unfortunately, does not have the concept of a null value. A field which has not had a value written to it instead contains an empty string. In order to make this behavior more web-developer-friendly, Eloquent FileMaker automatically converts the value of `''` in a FileMaker field to `null` when reading data from the Data API.
121+
122+
If you would like to have empty FileMaker fields returned as empty strings you can set the `empty_strings_to_null` config value to false in your `config/eloquent-filemaker.php` file. The config file can be published to your config folder by running `artisan vendor:publish --tag=eloquent-filemaker-config`.
123+
124+
Eloquent FileMaker will always automatically convert `null` values to `''` when writing data back to your FileMaker database to prevent errors.
125+
122126
### Read-only fields
123127
Many fields in your FileMaker database will be read-only, such as summaries and calculations, though you'll still want to get them when retrieving data from your database. FMModels will attempt to write all modified attributes back to your FileMaker database. If you write a read-only field, such as a calculation field, you will receive an error when attempting to write the field back to your FileMaker database.
124128

src/Database/Eloquent/Concerns/FMHasAttributes.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public function setAttribute($key, $value)
4444
$value = $value ? 1 : 0;
4545
}
4646

47-
// FileMaker can't handle null, so change it to ''
48-
if (is_null($value)) {
49-
$value = '';
50-
}
51-
5247
$this->attributes[$key] = $value;
5348

5449
return $this;

src/Database/Eloquent/FMModel.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,19 @@ public static function createFromRecord($record)
120120
})->toArray();
121121
}
122122

123+
// check our config to see if we should map empty strings to null - users may decide they don't want this
124+
if (config('eloquent-filemaker.empty_strings_to_null', true)) {
125+
// map each value to null if it's an empty string
126+
$fieldData = collect($fieldData)->map(function ($value) {
127+
return $value === '' ? null : $value;
128+
})->toArray();
129+
}
130+
123131
// fill in the field data we've mapped and retrieved
124-
$instance = tap($instance)->forceFill($fieldData);
132+
$instance->forceFill($fieldData);
125133

126134
// fill in the portal data we've mapped and retrieved
127-
$instance = tap($instance)->forceFill($portalData);
135+
$instance->forceFill($portalData);
128136

129137
$recordId = $record['recordId'];
130138
$modId = $record['modId'];

src/Providers/FileMakerConnectionServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ public function boot(Kernel $kernel)
4949
{
5050
// add the middleware to the global middleware so that we always end the FileMaker session
5151
$kernel->pushMiddleware(EndSession::class);
52+
53+
$this->publishes([
54+
__DIR__ . '/../config/eloquent-filemaker.php' => config_path('eloquent-filemaker.php'),
55+
], 'eloquent-filemaker-config');
5256
}
5357
}

src/Services/FileMakerConnection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,7 @@ public function buildPostDataFromQuery(FMBaseBuilder $query)
522522
}
523523

524524
/**
525-
* Strip out containers and read-only fields to prepare for a write query
526-
* OR - do the opposite and get ONLY containers
525+
* Strip out containers and read-only fields, convert null values to empty strings to prepare for a write query
527526
*
528527
* @return Collection
529528
*/
@@ -538,6 +537,11 @@ protected function getNonContainerFieldsForRecordWrite($fieldArray)
538537
if ($this->isContainer($field)) {
539538
$fieldData->forget($key);
540539
}
540+
541+
// set any null value to an empty string - FileMaker doesn't use true null values
542+
if ($field === null) {
543+
$fieldData->put($key, '');
544+
}
541545
}
542546

543547
return $fieldData;

src/config/eloquent-filemaker.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'empty_strings_to_null' => true,
5+
];

0 commit comments

Comments
 (0)