2007-09-20 19:02:57 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id: Export.php 2552 2007-09-19 19:33:00Z Jonathan.Wage $
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-09-20 19:02:57 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_Data_Export
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Data
|
2007-09-20 19:02:57 +04:00
|
|
|
* @author Jonathan H. Wage <jwage@mac.com>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2007-09-20 19:02:57 +04:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision: 2552 $
|
|
|
|
*/
|
|
|
|
class Doctrine_Data_Export extends Doctrine_Data
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* @param string $directory
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($directory)
|
|
|
|
{
|
|
|
|
$this->setDirectory($directory);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-20 19:02:57 +04:00
|
|
|
/**
|
|
|
|
* doExport
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function doExport()
|
|
|
|
{
|
|
|
|
$models = Doctrine::getLoadedModels();
|
|
|
|
$specifiedModels = $this->getModels();
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
$outputAll = true;
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
// for situation when the $models array is empty, but the $specifiedModels array isn't
|
|
|
|
if (empty($models)) {
|
2007-10-09 02:43:09 +04:00
|
|
|
$models = $specifiedModels;
|
|
|
|
}
|
|
|
|
|
2007-09-20 19:02:57 +04:00
|
|
|
foreach ($models AS $name) {
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! empty($specifiedModels) AND !in_array($name, $specifiedModels)) {
|
2007-09-20 19:02:57 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = new $name();
|
|
|
|
$table = $class->getTable();
|
|
|
|
$result = $table->findAll();
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! empty($result)) {
|
2007-09-20 19:02:57 +04:00
|
|
|
$data[$name] = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->prepareData($data);
|
|
|
|
|
|
|
|
return $this->dumpData($data);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-20 19:02:57 +04:00
|
|
|
/**
|
|
|
|
* dumpData
|
|
|
|
*
|
|
|
|
* Dump the prepared data to the fixtures files
|
|
|
|
*
|
|
|
|
* @param string $array
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dumpData(array $data)
|
|
|
|
{
|
|
|
|
$directory = $this->getDirectory();
|
|
|
|
$format = $this->getFormat();
|
|
|
|
|
|
|
|
if ($this->exportIndividualFiles()) {
|
|
|
|
|
|
|
|
if (is_array($directory)) {
|
|
|
|
throw new Doctrine_Data_Exception('You must specify a single path to a folder in order to export individual files.');
|
2007-10-21 10:23:59 +04:00
|
|
|
} else if ( ! is_dir($directory) && is_file($directory)) {
|
2007-10-16 06:37:31 +04:00
|
|
|
$directory = dirname($directory);
|
2007-09-20 19:02:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($data as $className => $classData) {
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! empty($classData)) {
|
2007-10-16 06:37:31 +04:00
|
|
|
Doctrine_Parser::dump(array($className => $classData), $format, $directory.DIRECTORY_SEPARATOR.$className.'.'.$format);
|
|
|
|
}
|
2007-09-20 19:02:57 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (is_dir($directory)) {
|
2007-10-23 04:37:39 +04:00
|
|
|
$directory .= DIRECTORY_SEPARATOR . 'data.' . $format;
|
2007-09-20 19:02:57 +04:00
|
|
|
}
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! empty($data)) {
|
2007-10-16 06:37:31 +04:00
|
|
|
return Doctrine_Parser::dump($data, $format, $directory);
|
|
|
|
}
|
2007-09-20 19:02:57 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-20 19:02:57 +04:00
|
|
|
/**
|
|
|
|
* prepareData
|
|
|
|
*
|
|
|
|
* Prepare the raw data to be exported with the parser
|
|
|
|
*
|
|
|
|
* @param string $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function prepareData($data)
|
|
|
|
{
|
|
|
|
$preparedData = array();
|
|
|
|
|
|
|
|
foreach ($data AS $className => $classData) {
|
|
|
|
|
|
|
|
foreach ($classData as $record) {
|
|
|
|
$className = get_class($record);
|
|
|
|
$recordKey = $className . '_' . implode('_', $record->identifier());
|
|
|
|
|
|
|
|
$recordData = $record->toArray();
|
|
|
|
|
|
|
|
foreach ($recordData as $key => $value) {
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $value) {
|
2007-09-20 19:02:57 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-10-09 02:47:59 +04:00
|
|
|
// skip single primary keys, we need to maintain composite primary keys
|
2008-02-24 23:31:49 +03:00
|
|
|
$keys = (array)$record->getTable()->getIdentifier();
|
2007-10-09 02:47:59 +04:00
|
|
|
|
|
|
|
if (count($keys) <= 1 && in_array($key, $keys)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-20 19:02:57 +04:00
|
|
|
if ($relation = $this->isRelation($record, $key)) {
|
|
|
|
$relationAlias = $relation['alias'];
|
|
|
|
$relationRecord = $record->$relationAlias;
|
|
|
|
|
|
|
|
// If collection then get first so we have an instance of the related record
|
|
|
|
if ($relationRecord instanceof Doctrine_Collection) {
|
|
|
|
$relationRecord = $relationRecord->getFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If relation is null or does not exist then continue
|
|
|
|
if ($relationRecord instanceof Doctrine_Null || !$relationRecord) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get class name for relation
|
|
|
|
$relationClassName = get_class($relationRecord);
|
|
|
|
|
|
|
|
$relationValue = $relationClassName . '_' . $value;
|
|
|
|
|
2007-11-15 22:46:17 +03:00
|
|
|
$preparedData[$className][$recordKey][$relationAlias] = $relationValue;
|
2007-10-09 02:47:59 +04:00
|
|
|
} else {
|
2007-09-20 19:02:57 +04:00
|
|
|
$preparedData[$className][$recordKey][$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $preparedData;
|
|
|
|
}
|
|
|
|
}
|