Skip to content

Commit

Permalink
Add ability to use complex data types and casts
Browse files Browse the repository at this point in the history
Currently Sushi inserts rows straight into the database, which does not allow any "complex" data to be saved in rows, not even an array. Think of the use case 
[ "county" => "SomeCounty", "postCodes"=>[111,222,333]]

This change adds the ability to use $casts, which allows Eloquent to cast arrays and other data as necessary into/from a string for saving to the database.
  • Loading branch information
eta-orionis authored Feb 2, 2024
1 parent b362dd4 commit b27ef2d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Sushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,18 @@ public function migrate()
$this->createTableWithNoData($tableName);
}

foreach (array_chunk($rows, $this->getSushiInsertChunkSize()) ?? [] as $inserts) {
if (!empty($inserts)) {
static::insert($inserts);
if (count($this->casts) === 0) {
foreach (array_chunk($rows, $this->getSushiInsertChunkSize()) ?? [] as $inserts) {
if (!empty($inserts)) {
static::insert($inserts);
}
}
} else { //casts are necessary, create each model singly so Eloquent can cast attributes as necessary
foreach ($rows as $row) {
static::create($row);
}
}

}

public function createTable(string $tableName, $firstRow)
Expand Down

0 comments on commit b27ef2d

Please sign in to comment.