Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Faker\ORM\Propel2;
4
 
5
use Propel\Runtime\Propel;
6
use Propel\Runtime\ServiceContainer\ServiceContainerInterface;
7
 
8
/**
9
 * Service class for populating a database using the Propel ORM.
10
 * A Populator can populate several tables using ActiveRecord classes.
11
 */
12
class Populator
13
{
14
    protected $generator;
15
    protected $entities = [];
16
    protected $quantities = [];
17
 
18
    public function __construct(\Faker\Generator $generator)
19
    {
20
        $this->generator = $generator;
21
    }
22
 
23
    /**
24
     * Add an order for the generation of $number records for $entity.
25
     *
26
     * @param mixed $entity A Propel ActiveRecord classname, or a \Faker\ORM\Propel2\EntityPopulator instance
27
     * @param int   $number The number of entities to populate
28
     */
29
    public function addEntity($entity, $number, $customColumnFormatters = [], $customModifiers = [])
30
    {
31
        if (!$entity instanceof \Faker\ORM\Propel2\EntityPopulator) {
32
            $entity = new \Faker\ORM\Propel2\EntityPopulator($entity);
33
        }
34
        $entity->setColumnFormatters($entity->guessColumnFormatters($this->generator));
35
 
36
        if ($customColumnFormatters) {
37
            $entity->mergeColumnFormattersWith($customColumnFormatters);
38
        }
39
        $entity->setModifiers($entity->guessModifiers($this->generator));
40
 
41
        if ($customModifiers) {
42
            $entity->mergeModifiersWith($customModifiers);
43
        }
44
        $class = $entity->getClass();
45
        $this->entities[$class] = $entity;
46
        $this->quantities[$class] = $number;
47
    }
48
 
49
    /**
50
     * Populate the database using all the Entity classes previously added.
51
     *
52
     * @param PropelPDO $con A Propel connection object
53
     *
54
     * @return array A list of the inserted PKs
55
     */
56
    public function execute($con = null)
57
    {
58
        if (null === $con) {
59
            $con = $this->getConnection();
60
        }
61
        $isInstancePoolingEnabled = Propel::isInstancePoolingEnabled();
62
        Propel::disableInstancePooling();
63
        $insertedEntities = [];
64
        $con->beginTransaction();
65
 
66
        foreach ($this->quantities as $class => $number) {
67
            for ($i = 0; $i < $number; ++$i) {
68
                $insertedEntities[$class][] = $this->entities[$class]->execute($con, $insertedEntities);
69
            }
70
        }
71
        $con->commit();
72
 
73
        if ($isInstancePoolingEnabled) {
74
            Propel::enableInstancePooling();
75
        }
76
 
77
        return $insertedEntities;
78
    }
79
 
80
    protected function getConnection()
81
    {
82
        // use the first connection available
83
        $class = key($this->entities);
84
 
85
        if (!$class) {
86
            throw new \RuntimeException('No class found from entities. Did you add entities to the Populator ?');
87
        }
88
 
89
        $peer = $class::TABLE_MAP;
90
 
91
        return Propel::getConnection($peer::DATABASE_NAME, ServiceContainerInterface::CONNECTION_WRITE);
92
    }
93
}