forked from fzaninotto/Faker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f4f69a
commit 3e6def3
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Faker\ORM; | ||
|
||
class Propel | ||
{ | ||
protected $class; | ||
protected $columnFormatters = array(); | ||
|
||
public function __construct($class) | ||
{ | ||
$this->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(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
require_once dirname(__FILE__) . '/../../Propel/runtime/lib/Propel.php'; | ||
set_include_path(dirname(__FILE__) . '/../../Propel/test/fixtures/bookstore/build/classes' . PATH_SEPARATOR . get_include_path()); | ||
Propel::init(dirname(__FILE__) . '/../../Propel/test/fixtures/bookstore/build/conf/bookstore-conf.php'); | ||
|
||
require_once '../src/Faker/ORM/Propel.php'; | ||
require_once '../src/Faker/Factory.php'; | ||
|
||
$entities = array(); | ||
$generator = Faker\Factory::create(); | ||
AuthorQuery::create()->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(); |