From 85c025db66c0abfe472affa8e082511750a5a2cc Mon Sep 17 00:00:00 2001 From: Arif Hoque Date: Wed, 11 Mar 2026 16:07:43 +0600 Subject: [PATCH] feat: saveMany() now support timeStamps: --- .../InteractsWithModelQueryProcessing.php | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Phaseolies/Database/Entity/Query/InteractsWithModelQueryProcessing.php b/src/Phaseolies/Database/Entity/Query/InteractsWithModelQueryProcessing.php index 659e260..0c532b0 100644 --- a/src/Phaseolies/Database/Entity/Query/InteractsWithModelQueryProcessing.php +++ b/src/Phaseolies/Database/Entity/Query/InteractsWithModelQueryProcessing.php @@ -282,8 +282,15 @@ public function getDirtyAttributes(): array */ public static function saveMany(array $rows, int $chunkSize = 100): int { - $filteredRows = array_map(function ($row) { - $model = new static(); + $model = new static(); + $usesTimestamps = $model->timeStamps; + $hasCastToDate = $usesTimestamps + ? (new static())->propertyHasAttribute(new static(), 'timeStamps', CastToDate::class) + : false; + + $dateTime = $hasCastToDate ? now()->startOfDay() : now(); + + $filteredRows = array_map(function ($row) use ($model, $usesTimestamps, $dateTime) { $creatable = $model->creatable; if (empty($creatable)) { @@ -293,7 +300,14 @@ public static function saveMany(array $rows, int $chunkSize = 100): int } } - return array_intersect_key($row, array_flip($creatable)); + $filtered = array_intersect_key($row, array_flip($creatable)); + + if ($usesTimestamps) { + $filtered['created_at'] = $filtered['created_at'] ?? $dateTime; + $filtered['updated_at'] = $dateTime; + } + + return $filtered; }, $rows); return static::query()->insertMany($filteredRows, $chunkSize);