3
* $Id: Export.php 2552 2007-09-19 19:33:00Z Jonathan.Wage $
5
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
* This software consists of voluntary contributions made by many individuals
18
* and is licensed under the LGPL. For more information, see
19
* <http://www.phpdoctrine.org>.
23
* Doctrine_Data_Export
27
* @author Jonathan H. Wage <jwage@mac.com>
28
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29
* @link www.phpdoctrine.org
31
* @version $Revision: 2552 $
33
class Doctrine_Data_Export extends Doctrine_Data
38
* @param string $directory
41
public function __construct($directory)
43
$this->setDirectory($directory);
49
* FIXME: This function has ugly hacks in it for temporarily disabling INDEXBY query parts of tables
52
* Update from jwage: I am not sure if their is any other better solution for this. It may be the correct
53
* solution to disable the indexBy settings for tables when exporting data fixtures. Maybe a better idea
54
* would be to extract this functionality to a pair of functions to enable/disable the index by settings
55
* so simply turn them on and off when they need to query for the translations standalone and don't need
56
* it to be indexed by the lang.
60
public function doExport()
62
$models = Doctrine::getLoadedModels();
63
$specifiedModels = $this->getModels();
67
// for situation when the $models array is empty, but the $specifiedModels array isn't
69
$models = $specifiedModels;
72
$models = Doctrine::initializeModels($models);
74
// temporarily disable indexBy query parts of selected and related tables
75
$originalIndexBy = array();
76
foreach ($models AS $name) {
77
$table = Doctrine::getTable($name);
78
if (!is_null($indexBy = $table->getBoundQueryPart('indexBy'))) {
79
$originalIndexBy[$name] = $indexBy;
80
$table->bindQueryPart('indexBy', null);
84
foreach ($models AS $name) {
85
if ( ! empty($specifiedModels) AND ! in_array($name, $specifiedModels)) {
89
$results = Doctrine::getTable($name)->findAll();
91
if ($results->count() > 0) {
92
$data[$name] = $results;
96
// Restore the temporarily disabled indexBy query parts
97
foreach($originalIndexBy AS $name => $indexBy) {
98
Doctrine::getTable($name)->bindQueryPart('indexBy', $indexBy);
101
$data = $this->prepareData($data);
103
return $this->dumpData($data);
109
* Dump the prepared data to the fixtures files
111
* @param string $array
114
public function dumpData(array $data)
116
$directory = $this->getDirectory();
117
$format = $this->getFormat();
119
if ($this->exportIndividualFiles()) {
120
if (is_array($directory)) {
121
throw new Doctrine_Data_Exception('You must specify a single path to a folder in order to export individual files.');
122
} else if ( ! is_dir($directory) && is_file($directory)) {
123
$directory = dirname($directory);
126
foreach ($data as $className => $classData) {
127
if ( ! empty($classData)) {
128
Doctrine_Parser::dump(array($className => $classData), $format, $directory.DIRECTORY_SEPARATOR.$className.'.'.$format);
132
if (is_dir($directory)) {
133
$directory .= DIRECTORY_SEPARATOR . 'data.' . $format;
136
if ( ! empty($data)) {
137
return Doctrine_Parser::dump($data, $format, $directory);
145
* Prepare the raw data to be exported with the parser
147
* @param string $data
150
public function prepareData($data)
152
$preparedData = array();
154
foreach ($data AS $className => $classData) {
155
$preparedData[$className] = array();
156
foreach ($classData as $record) {
157
$className = get_class($record);
158
$recordKey = $className . '_' . implode('_', $record->identifier());
159
$preparedData[$className][$recordKey] = array();
161
// skip single primary keys, we need to maintain composite primary keys
162
$keys = $record->getTable()->getIdentifier();
164
$recordData = $record->toArray(false);
166
foreach ($recordData as $key => $value) {
167
if ( ! is_array($keys)) {
168
$keys = array($keys);
171
if (count($keys) <= 1 && in_array($key, $keys)) {
175
if (is_object($record[$key])) {
176
// If the field is an object serialize it
177
$value = serialize($record[$key]);
180
if ($relation = $this->isRelation($record, $key)) {
184
$relationAlias = $relation['alias'];
185
$relationRecord = $record->$relationAlias;
187
// If collection then get first so we have an instance of the related record
188
if ($relationRecord instanceof Doctrine_Collection) {
189
$relationRecord = $relationRecord->getFirst();
192
// If relation is null or does not exist then continue
193
if ($relationRecord instanceof Doctrine_Null || ! $relationRecord) {
197
// Get class name for relation
198
$relationClassName = get_class($relationRecord);
200
$relationValue = $relationClassName . '_' . $value;
202
$preparedData[$className][$recordKey][$relationAlias] = $relationValue;
203
} else if ($record->getTable()->hasField($key)) {
204
$preparedData[$className][$recordKey][$key] = $value;
210
return $preparedData;
b'\\ No newline at end of file'