Created
August 8, 2016 17:57
-
-
Save lira92/4b98d6ef6114d5f664e6d6a6aadfbb4e 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
That's the sort of thing.
This is how I would done it. Moved the
run()
to the abstract class, reducing the amount needed to be edited in the seeder.