Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to dynamically load data. #117

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 21 additions & 1 deletion src/Sushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
trait Sushi
{
protected static $sushiConnection;
protected static $loadedData = null;

public function getRows()
{
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}
}
}
58 changes: 39 additions & 19 deletions tests/SushiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public function setUp(): void
}

Foo::resetStatics();
FooAssorted::resetStatics();
Bar::resetStatics();
File::cleanDirectory($this->cachePath);
}

public function tearDown(): void
{
Foo::resetStatics();
FooAssorted::resetStatics();
Bar::resetStatics();
File::cleanDirectory($this->cachePath);

Expand All @@ -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()
{
Expand Down Expand Up @@ -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())
);
}
Expand Down Expand Up @@ -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([]);
}
}

Expand Down Expand Up @@ -298,6 +328,7 @@ protected function afterMigrate(Blueprint $table)
class Bar extends Model
{
use \Sushi\Sushi;
use ResetTrait;

public static $hasBeenAccessedBefore = false;

Expand All @@ -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
Expand Down
Loading