~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to models/doctrine/lib/Doctrine/Data/Export.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 *  $Id: Export.php 2552 2007-09-19 19:33:00Z Jonathan.Wage $
 
4
 *
 
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.
 
16
 *
 
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>.
 
20
 */
 
21
 
 
22
/**
 
23
 * Doctrine_Data_Export
 
24
 *
 
25
 * @package     Doctrine
 
26
 * @subpackage  Data
 
27
 * @author      Jonathan H. Wage <jwage@mac.com>
 
28
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 
29
 * @link        www.phpdoctrine.org
 
30
 * @since       1.0
 
31
 * @version     $Revision: 2552 $
 
32
 */
 
33
class Doctrine_Data_Export extends Doctrine_Data
 
34
{
 
35
    /**
 
36
     * constructor
 
37
     *
 
38
     * @param string $directory 
 
39
     * @return void
 
40
     */
 
41
    public function __construct($directory)
 
42
    {
 
43
        $this->setDirectory($directory);
 
44
    }
 
45
 
 
46
    /**
 
47
     * doExport
 
48
     *
 
49
     * FIXME: This function has ugly hacks in it for temporarily disabling INDEXBY query parts of tables 
 
50
     * to export.
 
51
     *
 
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.
 
57
     *
 
58
     * @return void
 
59
     */
 
60
    public function doExport()
 
61
    {
 
62
        $models = Doctrine::getLoadedModels();
 
63
        $specifiedModels = $this->getModels();
 
64
 
 
65
        $data = array();
 
66
 
 
67
                    // for situation when the $models array is empty, but the $specifiedModels array isn't
 
68
        if (empty($models)) {
 
69
          $models = $specifiedModels;
 
70
        }
 
71
 
 
72
        $models = Doctrine::initializeModels($models);
 
73
 
 
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);
 
81
          }
 
82
        }
 
83
 
 
84
        foreach ($models AS $name) {
 
85
            if ( ! empty($specifiedModels) AND ! in_array($name, $specifiedModels)) {
 
86
                continue;
 
87
            }
 
88
 
 
89
            $results = Doctrine::getTable($name)->findAll();
 
90
 
 
91
            if ($results->count() > 0) {
 
92
                $data[$name] = $results;
 
93
            }
 
94
        }
 
95
 
 
96
        // Restore the temporarily disabled indexBy query parts
 
97
        foreach($originalIndexBy AS $name => $indexBy) {
 
98
            Doctrine::getTable($name)->bindQueryPart('indexBy', $indexBy);
 
99
        }
 
100
 
 
101
        $data = $this->prepareData($data);
 
102
 
 
103
        return $this->dumpData($data);
 
104
    }
 
105
 
 
106
    /**
 
107
     * dumpData
 
108
     *
 
109
     * Dump the prepared data to the fixtures files
 
110
     *
 
111
     * @param string $array 
 
112
     * @return void
 
113
     */
 
114
    public function dumpData(array $data)
 
115
    {
 
116
        $directory = $this->getDirectory();
 
117
        $format = $this->getFormat();
 
118
 
 
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);
 
124
            }
 
125
 
 
126
            foreach ($data as $className => $classData) {
 
127
                if ( ! empty($classData)) {
 
128
                    Doctrine_Parser::dump(array($className => $classData), $format, $directory.DIRECTORY_SEPARATOR.$className.'.'.$format);
 
129
                }
 
130
            }
 
131
        } else {
 
132
            if (is_dir($directory)) {
 
133
                $directory .= DIRECTORY_SEPARATOR . 'data.' . $format;
 
134
            }
 
135
 
 
136
            if ( ! empty($data)) {
 
137
                return Doctrine_Parser::dump($data, $format, $directory);
 
138
            }
 
139
        }
 
140
    }
 
141
 
 
142
    /**
 
143
     * prepareData
 
144
     *
 
145
     * Prepare the raw data to be exported with the parser
 
146
     *
 
147
     * @param string $data 
 
148
     * @return array
 
149
     */
 
150
    public function prepareData($data)
 
151
    {
 
152
        $preparedData = array();
 
153
 
 
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();
 
160
 
 
161
                // skip single primary keys, we need to maintain composite primary keys
 
162
                $keys = $record->getTable()->getIdentifier();
 
163
 
 
164
                $recordData = $record->toArray(false);
 
165
 
 
166
                foreach ($recordData as $key => $value) {
 
167
                    if ( ! is_array($keys)) {
 
168
                      $keys = array($keys);
 
169
                    }
 
170
 
 
171
                    if (count($keys) <= 1 && in_array($key, $keys)) {
 
172
                        continue;
 
173
                    }
 
174
 
 
175
                    if (is_object($record[$key])) {
 
176
                        // If the field is an object serialize it
 
177
                        $value = serialize($record[$key]);
 
178
                    }
 
179
 
 
180
                    if ($relation = $this->isRelation($record, $key)) {
 
181
                        if ( ! $value) {
 
182
                            continue;
 
183
                        }
 
184
                        $relationAlias = $relation['alias'];
 
185
                        $relationRecord = $record->$relationAlias;
 
186
 
 
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();
 
190
                        }
 
191
 
 
192
                        // If relation is null or does not exist then continue
 
193
                        if ($relationRecord instanceof Doctrine_Null || ! $relationRecord) {
 
194
                            continue;
 
195
                        }
 
196
 
 
197
                        // Get class name for relation
 
198
                        $relationClassName = get_class($relationRecord);
 
199
 
 
200
                        $relationValue = $relationClassName . '_' . $value;
 
201
 
 
202
                        $preparedData[$className][$recordKey][$relationAlias] = $relationValue;
 
203
                    } else if ($record->getTable()->hasField($key)) {                        
 
204
                        $preparedData[$className][$recordKey][$key] = $value;
 
205
                    }
 
206
                }
 
207
            }
 
208
        }
 
209
        
 
210
        return $preparedData;
 
211
    }
 
212
}
 
 
b'\\ No newline at end of file'