diff --git a/src/Faker/ORM/Propel.php b/src/Faker/ORM/Propel.php new file mode 100644 index 00000000..c0c5700b --- /dev/null +++ b/src/Faker/ORM/Propel.php @@ -0,0 +1,44 @@ +class = $class; + } + + public function setColumnFormatters($columnFormatters) + { + $this->columnFormatters = array_merge($columnFormatters, $this->columnFormatters); + } + + public function populate($nb = 100, &$insertedEntities = array()) + { + $pks = array(); + $class = $this->class; + $peer = $class::PEER; + $con = \Propel::getConnection($peer::DATABASE_NAME, \Propel::CONNECTION_WRITE); + $con->beginTransaction(); + for ($i=0; $i < $nb; $i++) { + $insertedEntities[$this->class][]= $this->populateOne($con, $insertedEntities); + } + $con->commit(); + } + + public function populateOne($con, $insertedEntities) + { + $obj = new $this->class(); + foreach ($this->columnFormatters as $column => $format) { + if (null !== $column) { + $obj->setByName($column, is_callable($format) ? $format($insertedEntities) : $format); + } + } + $obj->save($con); + return $obj->getPrimaryKey(); + } +} \ No newline at end of file diff --git a/test/test2.php b/test/test2.php new file mode 100644 index 00000000..9aae3889 --- /dev/null +++ b/test/test2.php @@ -0,0 +1,27 @@ +deleteAll(); +$populator = new Faker\ORM\Propel('Author'); +$authorColumnFormatters = array( + 'FirstName' => function() use ($generator) { return $generator->firstName; }, + 'LastName' => function() use ($generator) { return $generator->lastName; } +); +$populator->setColumnFormatters($authorColumnFormatters); +$populator->populate(10, $entities); +$bookColumnFormatters = array( + 'Title' => function() use ($generator) { return $generator->sentence; }, + 'AuthorId' => function($inserted) { return $inserted['Author'][mt_rand(0, count($inserted['Author']) - 1)]; } +); +$populator = new Faker\ORM\Propel('Book'); +$populator->setColumnFormatters($bookColumnFormatters); +$inserted = $populator->populate(10, $entities); +echo BookQuery::create()->joinWith('Book.Author')->find(); \ No newline at end of file