Skip to content

Commit b0fb1e2

Browse files
committed
refactor
1 parent b27ef2d commit b0fb1e2

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/Sushi.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,18 @@ public function migrate()
133133
$this->createTableWithNoData($tableName);
134134
}
135135

136-
if (count($this->casts) === 0) {
136+
if (count($this->casts) > 0) {
137+
foreach ($rows as $row) {
138+
// If $casts are present, use Eloquent's "create" instead of a plain insert so they are used and applied...
139+
static::forceCreate($row);
140+
}
141+
} else {
137142
foreach (array_chunk($rows, $this->getSushiInsertChunkSize()) ?? [] as $inserts) {
138-
if (!empty($inserts)) {
143+
if (! empty($inserts)) {
139144
static::insert($inserts);
140145
}
141146
}
142-
} else { //casts are necessary, create each model singly so Eloquent can cast attributes as necessary
143-
foreach ($rows as $row) {
144-
static::create($row);
145-
}
146147
}
147-
148148
}
149149

150150
public function createTable(string $tableName, $firstRow)

tests/SushiTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ function columns_with_varying_types()
6060
$this->assertEquals(null, $row->null);
6161
}
6262

63+
/** @test */
64+
function model_with_casts()
65+
{
66+
$model = ModelWithCasts::first();
67+
68+
$this->assertTrue(is_array($model->is_array));
69+
$this->assertTrue(is_bool($model->is_boolean));
70+
}
71+
6372
/** @test */
6473
function model_with_custom_schema()
6574
{
@@ -373,3 +382,18 @@ public function maki()
373382
return $this->belongsTo(Maki::class);
374383
}
375384
}
385+
386+
387+
class ModelWithCasts extends Model
388+
{
389+
use \Sushi\Sushi;
390+
391+
protected $casts = [
392+
'is_array' => 'array',
393+
'is_boolean' => 'boolean',
394+
];
395+
396+
protected $rows = [
397+
['is_array' => [1, 2, 3], 'is_boolean' => true],
398+
];
399+
}

0 commit comments

Comments
 (0)