Skip to content

Commit fedfe4e

Browse files
committed
fix SQL Expressionable typecast
1 parent c7a049b commit fedfe4e

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/Persistence.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,7 @@ public function typecastSaveRow(Model $model, array $row): array
174174

175175
$field = $model->getField($fieldName);
176176

177-
// SQL Expression cannot be converted
178-
if (!$value instanceof \Atk4\Data\Persistence\Sql\Expressionable) {
179-
$value = $this->typecastSaveField($field, $value);
180-
}
177+
$value = $this->typecastSaveField($field, $value);
181178

182179
// check null values for mandatory fields
183180
if ($value === null && $field->mandatory) {
@@ -197,6 +194,10 @@ public function typecastSaveRow(Model $model, array $row): array
197194
* NOTE: Please DO NOT perform "actual" field mapping here, because data
198195
* may be "aliased" from SQL persistencies or mapped depending on persistence
199196
* driver.
197+
*
198+
* @param array<string, scalar|null> $row
199+
*
200+
* @return array<string, mixed>
200201
*/
201202
public function typecastLoadRow(Model $model, array $row): array
202203
{
@@ -232,11 +233,15 @@ public function typecastSaveField(Field $field, $value)
232233
return null;
233234
}
234235

236+
// SQL Expression cannot be converted
237+
if ($value instanceof Persistence\Sql\Expressionable) {
238+
return $value;
239+
}
240+
235241
try {
236242
$v = $this->_typecastSaveField($field, $value);
237-
if ($v !== null && !is_scalar($v) && !$v instanceof Persistence\Sql\Expressionable) { // @phpstan-ignore-line
238-
throw (new Exception('Unexpected non-scalar value'))
239-
->addMoreInfo('type', get_debug_type($v));
243+
if ($v !== null && !is_scalar($v)) { // @phpstan-ignore-line
244+
throw new Exception('Unexpected non-scalar value');
240245
}
241246

242247
return $v;
@@ -250,14 +255,16 @@ public function typecastSaveField(Field $field, $value)
250255
* Cast specific field value from the way how it's stored inside
251256
* persistence to a PHP format.
252257
*
253-
* @param mixed $value
258+
* @param scalar|null $value
254259
*
255260
* @return mixed
256261
*/
257262
public function typecastLoadField(Field $field, $value)
258263
{
259264
if ($value === null) {
260265
return null;
266+
} elseif (!is_scalar($value)) {
267+
throw new Exception('Unexpected non-scalar value');
261268
}
262269

263270
try {
@@ -274,7 +281,7 @@ public function typecastLoadField(Field $field, $value)
274281
*
275282
* @param mixed $value
276283
*
277-
* @return scalar|Persistence\Sql\Expressionable|null
284+
* @return scalar|null
278285
*/
279286
protected function _typecastSaveField(Field $field, $value)
280287
{
@@ -310,7 +317,7 @@ protected function _typecastSaveField(Field $field, $value)
310317
* This is the actual field typecasting, which you can override in your
311318
* persistence to implement necessary typecasting.
312319
*
313-
* @param mixed $value
320+
* @param scalar|null $value
314321
*
315322
* @return mixed
316323
*/

0 commit comments

Comments
 (0)