-
-
Save pimeo/325b5b2d97ef50d3581b06e60cc8d151 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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