Skip to content

Instantly share code, notes, and snippets.

@pimeo
Forked from lira92/AbstractSeedFromJson.php
Created September 16, 2019 10:15
Show Gist options
  • Save pimeo/325b5b2d97ef50d3581b06e60cc8d151 to your computer and use it in GitHub Desktop.
Save pimeo/325b5b2d97ef50d3581b06e60cc8d151 to your computer and use it in GitHub Desktop.
<?php
namespace App\Migrations;
use Phinx\Seed\AbstractSeed;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
class AbstractSeedFromJson extends AbstractSeed
{
/**
* @var String
*/
protected $jsonFileName;
/**
* @var Array
*/
protected $externalData;
public function setJsonFileName($file)
{
if(!(new FileSystem)->exists($file)) {
throw new FileNotFoundException(sprintf('Failed to set JsonFileName because file "%s" does not exist.', $file), 0, null, $file);
}
$this->jsonFileName = $file;
$this->loadExternalFile();
}
public function getExternalData()
{
return $this->externalData;
}
private function loadExternalFile()
{
$this->externalData = json_decode(file_get_contents($this->jsonFileName), true);
}
}
<?php
use App\Migrations\AbstractSeedFromJson;
class UfSeeder extends AbstractSeedFromJson
{
public function init()
{
$this->setJsonFileName(__DIR__ . DIRECTORY_SEPARATOR . 'uf.json');
}
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
$estados = $this->table('aux_estados');
$estados->insert($this->getExternalData())
->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment