Created
May 10, 2015 22:13
-
-
Save jatubio/10dd29cd357e36ff26a3 to your computer and use it in GitHub Desktop.
Solución alternativa a la lección 1.9 Seeders con llaves foráneas
This file contains 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 Illuminate\Database\Seeder; | |
/** | |
* Created by PhpStorm. | |
* User: kali | |
* Date: 10/05/2015 | |
* Time: 13:57 | |
*/ | |
abstract class BaseSeeder extends Seeder { | |
protected static $poolSeeders=[]; | |
abstract public function getModel(); | |
abstract public function getDummyData(\Faker\Generator $faker, array $customValues = [ ]); | |
public function __construct() | |
{ | |
$this->addSeederToPool($this->getModel()); | |
} | |
protected function createMultipleEntities($numberOfEntities, array $customValues = [ ]) | |
{ | |
for ($entitiesCounter = 1; $entitiesCounter <= $numberOfEntities; $entitiesCounter++) | |
{ | |
$this->createEntity($customValues); | |
} | |
} | |
protected function getRandomEntityFromModel($modelName) | |
{ | |
return $this->getSeederFromPool($modelName)->getRandomEntity(); | |
} | |
private function addSeederToPool($model) | |
{ | |
$modelName = basename(get_class($model)); | |
static::$poolSeeders[$modelName]=$this; | |
return $this; | |
} | |
private function getSeederFromPool($modelName) | |
{ | |
if ( ! $this->existsModelInSeedersPool($modelName) ) | |
{ | |
throw new OutOfRangeException ("The model $modelName does not exist in seeders pool"); | |
} | |
return static::$poolSeeders[$modelName]; | |
} | |
private function existsModelInSeedersPool($modelName) | |
{ | |
return array_key_exists($modelName,static::$poolSeeders); | |
} | |
// Note: Duplicate number of new entities on $seederClass | |
protected function getEntityFromSeederClass($seederClass, array $customValues = [ ]) | |
{ | |
$seeder = new $seederClass; | |
return $seeder->createEntity($customValues); | |
} | |
} |
This file contains 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 | |
/** | |
* Created by PhpStorm. | |
* User: kali | |
* Date: 10/05/2015 | |
* Time: 23:48 | |
*/ | |
use Illuminate\Database\Eloquent\Collection; | |
use Faker\Factory as Faker; | |
trait SeederTrait { | |
private static $poolEntities; | |
public function __construct() | |
{ | |
static::$poolEntities = new Collection(); | |
parent::__construct(); | |
} | |
protected function createEntity(array $customValues = [ ]) | |
{ | |
$dummyData = $this->getDummyData(Faker::create('es_ES'), $customValues); | |
$dummyData = array_merge($dummyData, $customValues); | |
return $this->addEntityToPool($this->getModel()->create($dummyData)); | |
} | |
protected function getRandomEntity() | |
{ | |
return static::$poolEntities->random(); | |
} | |
private function addEntityToPool($entity) | |
{ | |
static::$poolEntities->add($entity); | |
return $entity; | |
} | |
} |
This file contains 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 TeachMe\Entities\Ticket; | |
/** | |
* Created by PhpStorm. | |
* User: kali | |
* Date: 10/05/2015 | |
* Time: 14:59 | |
*/ | |
class TicketTableSeeder extends BaseSeeder { | |
use SeederTrait; | |
public function run() | |
{ | |
$this->createMultipleEntities(50); | |
} | |
public function getModel() | |
{ | |
return new Ticket(); | |
} | |
public function getDummyData(\Faker\Generator $faker, array $customValues = [ ]) | |
{ | |
return [ | |
'title' => $faker->sentence(), | |
'status' => $faker->randomElement([ 'open', 'open', 'closed' ]), | |
'user_id' => $this->getRandomEntityFromModel('User')->id | |
]; | |
} | |
} |
This file contains 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 Faker\Generator; | |
use TeachMe\Entities\User; | |
class UserTableSeeder extends BaseSeeder { | |
use SeederTrait; | |
public function run() | |
{ | |
$this->createAdmin(); | |
$this->createMultipleEntities(50); | |
} | |
public function getModel() | |
{ | |
return new User(); | |
} | |
public function getDummyData(Generator $faker, array $customValues = [ ]) | |
{ | |
return [ | |
'name' => $faker->name, | |
'email' => $faker->email, | |
'password' => bcrypt('secret') | |
]; | |
} | |
private function createAdmin() | |
{ | |
User::create([ | |
'name' => 'Juan Antonio Tubío', | |
'email' => '[email protected]', | |
'password' => bcrypt('admin') | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola, estoy siguiento un tutorial y este codigo me parecio una buena alternativa.
Tengo solo un problema en la Linea:
'user_id' => $this->getRandomEntityFromModel('User')->id
En donde me arroja el error:
[OutOfRangeException]
The model User does not exist in seeders pool
Me podrias indicar si sabes el porque del error?