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