1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of the sfPropel13Plugin package. |
---|
4 | * (c) 2007 Joshua May <notjosh@gmail.com> |
---|
5 | * |
---|
6 | * For the full copyright and license information, please view the LICENSE |
---|
7 | * file that was distributed with this source code. |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * This class is the Propel 1.2 implementation of sfPropelData. It interacts with the data source |
---|
12 | * and loads data. |
---|
13 | * |
---|
14 | * @package symfony |
---|
15 | * @subpackage addon |
---|
16 | * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
17 | * @version SVN: $Id: sfPropelData.class.php 4940 2007-08-30 14:11:28Z fabien $ |
---|
18 | */ |
---|
19 | class sfPropel13Data extends sfPropelData |
---|
20 | { |
---|
21 | |
---|
22 | /** |
---|
23 | * @see sfPropelData::loadData() |
---|
24 | */ |
---|
25 | public function loadData($directory_or_file = null, $connectionName = 'propel') |
---|
26 | { |
---|
27 | $fixture_files = $this->getFiles($directory_or_file); |
---|
28 | |
---|
29 | // wrap all database operations in a single transaction |
---|
30 | $this->con = Propel::getConnection($connectionName); |
---|
31 | try |
---|
32 | { |
---|
33 | $this->con->beginTransaction(); |
---|
34 | |
---|
35 | $this->doDeleteCurrentData($fixture_files); |
---|
36 | |
---|
37 | $this->doLoadData($fixture_files); |
---|
38 | |
---|
39 | $this->con->commit(); |
---|
40 | } |
---|
41 | catch (Exception $e) |
---|
42 | { |
---|
43 | $this->con->rollback(); |
---|
44 | throw $e; |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * @see sfPropelData::dumpData() |
---|
50 | */ |
---|
51 | public function dumpData($directory_or_file = null, $tables = 'all', $connectionName = 'propel') |
---|
52 | { |
---|
53 | $sameFile = true; |
---|
54 | if (is_dir($directory_or_file)) |
---|
55 | { |
---|
56 | // multi files |
---|
57 | $sameFile = false; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | // same file |
---|
62 | // delete file |
---|
63 | } |
---|
64 | |
---|
65 | $this->con = Propel::getConnection($connectionName); |
---|
66 | |
---|
67 | // get tables |
---|
68 | if ('all' === $tables || is_null($tables)) |
---|
69 | { |
---|
70 | // load all map builder classes |
---|
71 | $files = sfFinder::type('file')->name('*MapBuilder.php')->in(sfLoader::getModelDirs()); |
---|
72 | foreach ($files as $file) |
---|
73 | { |
---|
74 | $mapBuilderClass = basename($file, '.php'); |
---|
75 | $map = new $mapBuilderClass(); |
---|
76 | $map->doBuild(); |
---|
77 | } |
---|
78 | |
---|
79 | $dbMap = Propel::getDatabaseMap($connectionName); |
---|
80 | $tables = array(); |
---|
81 | foreach ($dbMap->getTables() as $table) |
---|
82 | { |
---|
83 | $tables[] = $table->getPhpName(); |
---|
84 | } |
---|
85 | } |
---|
86 | else if (!is_array($tables)) |
---|
87 | { |
---|
88 | $tables = array($tables); |
---|
89 | } |
---|
90 | |
---|
91 | $dumpData = array(); |
---|
92 | |
---|
93 | // load map classes |
---|
94 | |
---|
95 | array_walk($tables, array($this, 'loadMapBuilder')); |
---|
96 | |
---|
97 | $tables = $this->fixOrderingOfForeignKeyData($tables); |
---|
98 | |
---|
99 | foreach ($tables as $tableName) |
---|
100 | { |
---|
101 | $tableMap = $this->maps[$tableName]->getDatabaseMap()->getTable(constant($tableName.'Peer::TABLE_NAME')); |
---|
102 | |
---|
103 | // get db info |
---|
104 | $stmt = $this->con->prepare('SELECT * FROM '.constant($tableName.'Peer::TABLE_NAME')); |
---|
105 | $stmt->execute(); |
---|
106 | |
---|
107 | while ($row = $stmt->fetch()) |
---|
108 | { |
---|
109 | $pk = $tableName; |
---|
110 | $values = array(); |
---|
111 | foreach ($tableMap->getColumns() as $column) |
---|
112 | { |
---|
113 | $col = strtolower($column->getColumnName()); |
---|
114 | if ($column->isPrimaryKey()) |
---|
115 | { |
---|
116 | $pk .= '_'.$row[$col]; |
---|
117 | } |
---|
118 | else if ($column->isForeignKey()) |
---|
119 | { |
---|
120 | $relatedTable = $this->maps[$tableName]->getDatabaseMap()->getTable($column->getRelatedTableName()); |
---|
121 | |
---|
122 | $values[$col] = $relatedTable->getPhpName().'_'.$row[$col]; |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | $values[$col] = $row[$col]; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | if (!isset($dumpData[$tableName])) |
---|
131 | { |
---|
132 | $dumpData[$tableName] = array(); |
---|
133 | } |
---|
134 | |
---|
135 | $dumpData[$tableName][$pk] = $values; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | // save to file(s) |
---|
140 | if ($sameFile) |
---|
141 | { |
---|
142 | file_put_contents($directory_or_file, Spyc::YAMLDump($dumpData)); |
---|
143 | } |
---|
144 | else |
---|
145 | { |
---|
146 | $i = 0; |
---|
147 | foreach ($tables as $tableName) |
---|
148 | { |
---|
149 | if (!isset($dumpData[$tableName])) |
---|
150 | { |
---|
151 | continue; |
---|
152 | } |
---|
153 | |
---|
154 | file_put_contents(sprintf("%s/%03d-%s.yml", $directory_or_file, ++$i, $tableName), Spyc::YAMLDump(array($tableName => $dumpData[$tableName]))); |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|