diff --git a/README.md b/README.md index 9717238..b612a13 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,37 @@ class Role extends Model } ``` +## Using ::loadData() +You can also load rows dynamically (once per instance) using `loadData()` static method. + +1. Implement the constructor changes into your model. + +```php +class Role extends Model +{ + use \Sushi\Sushi; + + public function __construct(array $rows = []) + { + // Injects data before the contructor + $this->setLoadedData($rows); + + parent::__construct([]); + } +} +``` + +2. Inject the rows through static call: + +```php + // Get instance of Eloquent Builder so you can use e.g. ->where() or ->get() + Role::loadData([ + ['id' => 1, 'label' => 'admin'], + ['id' => 2, 'label' => 'manager'], + ['id' => 3, 'label' => 'user'], + ]); +``` + ### Caching ->getRows() If you choose to use your own ->getRows() method, the rows will NOT be cached between requests by default. diff --git a/src/Sushi.php b/src/Sushi.php index 95daae2..4f24383 100644 --- a/src/Sushi.php +++ b/src/Sushi.php @@ -11,6 +11,7 @@ trait Sushi { protected static $sushiConnection; + protected static $loadedData = null; public function getRows() { @@ -124,7 +125,7 @@ protected static function setSqliteConnection($database) public function migrate() { - $rows = $this->getRows(); + $rows = $this->getLoadedData() ?? $this->getRows(); $tableName = $this->getTable(); if (count($rows)) { @@ -253,4 +254,23 @@ public function getConnectionName() { return static::class; } + + public static function loadData(array $rows) + { + static::clearBootedModels(); + + return (new static($rows))->newQuery(); + } + + public function getLoadedData(): ?array + { + return self::$loadedData; + } + + public function setLoadedData(array $rows): void + { + if (!empty($rows)) { + self::$loadedData = $rows; + } + } } diff --git a/tests/SushiTest.php b/tests/SushiTest.php index 6d16159..a2b3a3d 100644 --- a/tests/SushiTest.php +++ b/tests/SushiTest.php @@ -24,6 +24,7 @@ public function setUp(): void } Foo::resetStatics(); + FooAssorted::resetStatics(); Bar::resetStatics(); File::cleanDirectory($this->cachePath); } @@ -31,6 +32,7 @@ public function setUp(): void public function tearDown(): void { Foo::resetStatics(); + FooAssorted::resetStatics(); Bar::resetStatics(); File::cleanDirectory($this->cachePath); @@ -43,11 +45,27 @@ function basic_usage() $this->assertEquals(3, Foo::count()); $this->assertEquals('bar', Foo::first()->foo); $this->assertEquals('lob', Foo::whereBob('lob')->first()->bob); + + Bar::$hasBeenAccessedBefore = false; $this->assertEquals(2, Bar::count()); $this->assertEquals('bar', Bar::first()->foo); $this->assertEquals('lob', Bar::whereBob('lob')->first()->bob); } + /** @test */ + function load_data_usage() + { + $rows = [ + ['foo' => 'bar', 'bob' => 'lob'], + ['foo' => 'bar', 'bob' => 'lob'], + ['foo' => 'baz', 'bob' => 'law'], + ]; + + $this->assertEquals(3, FooAssorted::loadData($rows)->count()); + $this->assertEquals('bar', FooAssorted::loadData($rows)->first()->foo); + $this->assertEquals('lob', FooAssorted::loadData($rows)->whereBob('lob')->first()->bob); + } + /** @test */ function columns_with_varying_types() { @@ -96,7 +114,7 @@ function caches_sqlite_file_if_storage_cache_folder_is_available() $this->assertTrue(file_exists($this->cachePath)); $this->assertStringContainsString( - 'sushi/tests/cache/sushi-tests-foo.sqlite', + 'tests/cache/sushi-tests-foo.sqlite', str_replace('\\', '/', (new Foo())->getConnection()->getDatabaseName()) ); } @@ -218,25 +236,37 @@ protected function usesSqliteConnection($app) } } +trait ResetTrait +{ + public static function resetStatics() + { + static::resolveConnection(null); + static::clearBootedModels(); + } +} + class Foo extends Model { use \Sushi\Sushi; + use ResetTrait; protected $rows = [ ['foo' => 'bar', 'bob' => 'lob'], ['foo' => 'bar', 'bob' => 'lob'], ['foo' => 'baz', 'bob' => 'law'], ]; +} - public static function resetStatics() - { - static::setSushiConnection(null); - static::clearBootedModels(); - } +class FooAssorted extends Model +{ + use \Sushi\Sushi; + use ResetTrait; - public static function setSushiConnection($connection) + public function __construct(array $rows = []) { - static::$sushiConnection = $connection; + $this->setLoadedData($rows); + + parent::__construct([]); } } @@ -298,6 +328,7 @@ protected function afterMigrate(Blueprint $table) class Bar extends Model { use \Sushi\Sushi; + use ResetTrait; public static $hasBeenAccessedBefore = false; @@ -318,17 +349,6 @@ public function getRows() ]; } } - - public static function resetStatics() - { - static::setSushiConnection(null); - static::clearBootedModels(); - } - - public static function setSushiConnection($connection) - { - static::$sushiConnection = $connection; - } } class Baz extends Model