diff --git a/readme.md b/readme.md index acc5eb1588..5dce737cd2 100644 --- a/readme.md +++ b/readme.md @@ -363,7 +363,7 @@ You can check available Faker locales in the source code, [under the `Provider` ## Populating Entities Using an ORM or an ODM -Faker provides adapters for Object-Relational and Object-Document Mappers (currently, [Propel](http://www.propelorm.org), [Doctrine2](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/), [CakePHP](http://cakephp.org) and [Mandango](https://github.com/mandango/mandango) are supported). These adapters ease the population of databases through the Entity classes provided by an ORM library (or the population of document stores using Document classes provided by an ODM library). +Faker provides adapters for Object-Relational and Object-Document Mappers (currently, [Propel](http://www.propelorm.org), [Doctrine2](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/), [CakePHP](http://cakephp.org), [Spot2](https://github.com/vlucas/spot2) and [Mandango](https://github.com/mandango/mandango) are supported). These adapters ease the population of databases through the Entity classes provided by an ORM library (or the population of document stores using Document classes provided by an ODM library). To populate entities, create a new populator class (using a generator instance as parameter), then list the class and number of all the entities that must be generated. To launch the actual data population, call the `execute()` method. diff --git a/src/Faker/ORM/Spot/ColumnTypeGuesser.php b/src/Faker/ORM/Spot/ColumnTypeGuesser.php new file mode 100644 index 0000000000..59f574a7ce --- /dev/null +++ b/src/Faker/ORM/Spot/ColumnTypeGuesser.php @@ -0,0 +1,77 @@ +generator = $generator; + } + + /** + * @param array $field + * @return \Closure|null + */ + public function guessFormat(array $field) + { + $generator = $this->generator; + $type = $field['type']; + switch ($type) { + case 'boolean': + return function () use ($generator) { + return $generator->boolean; + }; + case 'decimal': + $size = isset($field['precision']) ? $field['precision'] : 2; + + return function () use ($generator, $size) { + return $generator->randomNumber($size + 2) / 100; + }; + case 'smallint': + return function () { + return mt_rand(0, 65535); + }; + case 'integer': + return function () { + return mt_rand(0, intval('2147483647')); + }; + case 'bigint': + return function () { + return mt_rand(0, intval('18446744073709551615')); + }; + case 'float': + return function () { + return mt_rand(0, intval('4294967295'))/mt_rand(1, intval('4294967295')); + }; + case 'string': + $size = isset($field['length']) ? $field['length'] : 255; + + return function () use ($generator, $size) { + return $generator->text($size); + }; + case 'text': + return function () use ($generator) { + return $generator->text; + }; + case 'datetime': + case 'date': + case 'time': + return function () use ($generator) { + return $generator->datetime; + }; + default: + // no smart way to guess what the user expects here + return null; + } + } +} diff --git a/src/Faker/ORM/Spot/EntityPopulator.php b/src/Faker/ORM/Spot/EntityPopulator.php new file mode 100644 index 0000000000..7de8a27cbb --- /dev/null +++ b/src/Faker/ORM/Spot/EntityPopulator.php @@ -0,0 +1,222 @@ +mapper = $mapper; + $this->locator = $locator; + $this->useExistingData = $useExistingData; + } + + /** + * @return string + */ + public function getMapper() + { + return $this->mapper; + } + + /** + * @param $columnFormatters + */ + public function setColumnFormatters($columnFormatters) + { + $this->columnFormatters = $columnFormatters; + } + + /** + * @return array + */ + public function getColumnFormatters() + { + return $this->columnFormatters; + } + + /** + * @param $columnFormatters + */ + public function mergeColumnFormattersWith($columnFormatters) + { + $this->columnFormatters = array_merge($this->columnFormatters, $columnFormatters); + } + + /** + * @param array $modifiers + */ + public function setModifiers(array $modifiers) + { + $this->modifiers = $modifiers; + } + + /** + * @return array + */ + public function getModifiers() + { + return $this->modifiers; + } + + /** + * @param array $modifiers + */ + public function mergeModifiersWith(array $modifiers) + { + $this->modifiers = array_merge($this->modifiers, $modifiers); + } + + /** + * @param Generator $generator + * @return array + */ + public function guessColumnFormatters(Generator $generator) + { + $formatters = array(); + $nameGuesser = new Name($generator); + $columnTypeGuesser = new ColumnTypeGuesser($generator); + $fields = $this->mapper->fields(); + foreach ($fields as $fieldName => $field) { + if ($field['primary'] == true) { + continue; + } + if ($formatter = $nameGuesser->guessFormat($fieldName)) { + $formatters[$fieldName] = $formatter; + continue; + } + if ($formatter = $columnTypeGuesser->guessFormat($field)) { + $formatters[$fieldName] = $formatter; + continue; + } + } + $entityName = $this->mapper->entity(); + $entity = $this->mapper->build([]); + $relations = $entityName::relations($this->mapper, $entity); + foreach ($relations as $relation) { + // We don't need any other relation here. + if ($relation instanceof BelongsTo) { + + $fieldName = $relation->localKey(); + $entityName = $relation->entityName(); + $field = $fields[$fieldName]; + $required = $field['required']; + + $locator = $this->locator; + + $formatters[$fieldName] = function ($inserted) use ($required, $entityName, $locator) { + if (!empty($inserted[$entityName])) { + return $inserted[$entityName][mt_rand(0, count($inserted[$entityName]) - 1)]->getId(); + } else { + if ($required && $this->useExistingData) { + // We did not add anything like this, but it's required, + // So let's find something existing in DB. + $mapper = $this->locator->mapper($entityName); + $records = $mapper->all()->limit(self::RELATED_FETCH_COUNT)->toArray(); + if (empty($records)) { + return null; + } + $id = $records[mt_rand(0, count($records) - 1)]['id']; + + return $id; + } else { + return null; + } + } + }; + + } + } + + return $formatters; + } + + /** + * Insert one new record using the Entity class. + * + * @param $insertedEntities + * @return string + */ + public function execute($insertedEntities) + { + $obj = $this->mapper->build([]); + + $this->fillColumns($obj, $insertedEntities); + $this->callMethods($obj, $insertedEntities); + + $this->mapper->insert($obj); + + + return $obj; + } + + /** + * @param $obj + * @param $insertedEntities + */ + private function fillColumns($obj, $insertedEntities) + { + foreach ($this->columnFormatters as $field => $format) { + if (null !== $format) { + $value = is_callable($format) ? $format($insertedEntities, $obj) : $format; + $obj->set($field, $value); + } + } + } + + /** + * @param $obj + * @param $insertedEntities + */ + private function callMethods($obj, $insertedEntities) + { + foreach ($this->getModifiers() as $modifier) { + $modifier($obj, $insertedEntities); + } + } +} diff --git a/src/Faker/ORM/Spot/Populator.php b/src/Faker/ORM/Spot/Populator.php new file mode 100644 index 0000000000..c834b04c8c --- /dev/null +++ b/src/Faker/ORM/Spot/Populator.php @@ -0,0 +1,88 @@ +generator = $generator; + $this->locator = $locator; + } + + /** + * Add an order for the generation of $number records for $entity. + * + * @param $entityName string Name of Entity object to generate + * @param $number int The number of entities to populate + * @param $customColumnFormatters array + * @param $customModifiers array + * @param $useExistingData bool Should we use existing rows (e.g. roles) to populate relations? + */ + public function addEntity( + $entityName, + $number, + $customColumnFormatters = array(), + $customModifiers = array(), + $useExistingData = false + ) { + $mapper = $this->locator->mapper($entityName); + if (null === $mapper) { + throw new \InvalidArgumentException("No mapper can be found for entity " . $entityName); + } + $entity = new EntityPopulator($mapper, $this->locator, $useExistingData); + + $entity->setColumnFormatters($entity->guessColumnFormatters($this->generator)); + if ($customColumnFormatters) { + $entity->mergeColumnFormattersWith($customColumnFormatters); + } + $entity->mergeModifiersWith($customModifiers); + + $this->entities[$entityName] = $entity; + $this->quantities[$entityName] = $number; + } + + /** + * Populate the database using all the Entity classes previously added. + * + * @param Locator $locator A Spot locator + * + * @return array A list of the inserted PKs + */ + public function execute($locator = null) + { + if (null === $locator) { + $locator = $this->locator; + } + if (null === $locator) { + throw new \InvalidArgumentException("No entity manager passed to Spot Populator."); + } + + $insertedEntities = array(); + foreach ($this->quantities as $entityName => $number) { + for ($i = 0; $i < $number; $i++) { + $insertedEntities[$entityName][] = $this->entities[$entityName]->execute( + $insertedEntities + ); + } + } + + return $insertedEntities; + } +}