Skip to content

Commit 7661897

Browse files
authored
feat: saveMany() now injects timestamps (#217)
2 parents ee9621c + 85c025d commit 7661897

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

src/Phaseolies/Database/Entity/Query/InteractsWithModelQueryProcessing.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,15 @@ public function getDirtyAttributes(): array
282282
*/
283283
public static function saveMany(array $rows, int $chunkSize = 100): int
284284
{
285-
$filteredRows = array_map(function ($row) {
286-
$model = new static();
285+
$model = new static();
286+
$usesTimestamps = $model->timeStamps;
287+
$hasCastToDate = $usesTimestamps
288+
? (new static())->propertyHasAttribute(new static(), 'timeStamps', CastToDate::class)
289+
: false;
290+
291+
$dateTime = $hasCastToDate ? now()->startOfDay() : now();
292+
293+
$filteredRows = array_map(function ($row) use ($model, $usesTimestamps, $dateTime) {
287294
$creatable = $model->creatable;
288295

289296
if (empty($creatable)) {
@@ -293,7 +300,14 @@ public static function saveMany(array $rows, int $chunkSize = 100): int
293300
}
294301
}
295302

296-
return array_intersect_key($row, array_flip($creatable));
303+
$filtered = array_intersect_key($row, array_flip($creatable));
304+
305+
if ($usesTimestamps) {
306+
$filtered['created_at'] = $filtered['created_at'] ?? $dateTime;
307+
$filtered['updated_at'] = $dateTime;
308+
}
309+
310+
return $filtered;
297311
}, $rows);
298312

299313
return static::query()->insertMany($filteredRows, $chunkSize);

0 commit comments

Comments
 (0)