1
0
mirror of synced 2024-12-14 15:16:04 +03:00
doctrine2/lib/Doctrine/Data.php

286 lines
6.7 KiB
PHP
Raw Normal View History

2007-09-20 19:02:57 +04:00
<?php
/*
* $Id: Data.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
* <http://www.phpdoctrine.org>.
2007-09-20 19:02:57 +04:00
*/
/**
* Doctrine_Data
*
* Base Doctrine_Data class for dumping and loading data to and from fixtures files.
* Support formats are based on what formats are available in Doctrine_Parser such as yaml, xml, json, etc.
2007-09-20 19:02:57 +04:00
*
* @package Doctrine
* @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
* @link www.phpdoctrine.org
2007-09-20 19:02:57 +04:00
* @since 1.0
* @version $Revision: 2552 $
*/
class Doctrine_Data
{
/**
* formats
*
* array of formats data can be in
*
* @var string
*/
2007-10-26 08:13:29 +04:00
protected $_formats = array('csv', 'yml', 'xml');
2007-09-20 19:02:57 +04:00
/**
* format
*
* the default and current format we are working with
*
* @var string
*/
2007-10-26 08:13:29 +04:00
protected $_format = 'yml';
2007-09-20 19:02:57 +04:00
/**
* directory
*
* array of directory/yml paths or single directory/yml file
*
* @var string
*/
2007-10-26 08:13:29 +04:00
protected $_directory = null;
2007-09-20 19:02:57 +04:00
/**
* models
*
* specified array of models to use
*
* @var string
*/
2007-10-26 08:13:29 +04:00
protected $_models = array();
2007-09-20 19:02:57 +04:00
/**
2007-10-26 08:13:29 +04:00
* _exportIndividualFiles
2007-09-20 19:02:57 +04:00
*
* whether or not to export data to individual files instead of 1
*
* @var string
*/
2007-10-26 08:13:29 +04:00
protected $_exportIndividualFiles = false;
2007-09-20 19:02:57 +04:00
/**
* setFormat
*
* Set the current format we are working with
*
* @param string $format
* @return void
*/
public function setFormat($format)
{
2007-10-26 08:13:29 +04:00
$this->_format = $format;
2007-09-20 19:02:57 +04:00
}
2007-09-20 19:02:57 +04:00
/**
* getFormat
*
* Get the current format we are working with
*
* @return void
*/
public function getFormat()
{
2007-10-26 08:13:29 +04:00
return $this->_format;
2007-09-20 19:02:57 +04:00
}
2007-09-20 19:02:57 +04:00
/**
* getFormats
*
* Get array of available formats
*
* @return void
2007-09-20 19:02:57 +04:00
*/
public function getFormats()
{
2007-10-26 08:13:29 +04:00
return $this->_formats;
2007-09-20 19:02:57 +04:00
}
2007-09-20 19:02:57 +04:00
/**
* setDirectory
*
* Set the array/string of directories or yml file paths
*
* @return void
2007-09-20 19:02:57 +04:00
*/
public function setDirectory($directory)
{
2007-10-26 08:13:29 +04:00
$this->_directory = $directory;
2007-09-20 19:02:57 +04:00
}
2007-09-20 19:02:57 +04:00
/**
* getDirectory
*
* Get directory for dumping/loading data from and to
2007-09-20 19:02:57 +04:00
*
* @return void
*/
public function getDirectory()
{
2007-10-26 08:13:29 +04:00
return $this->_directory;
2007-09-20 19:02:57 +04:00
}
2007-09-20 19:02:57 +04:00
/**
* setModels
*
* Set the array of specified models to work with
*
* @param string $models
* @return void
*/
public function setModels($models)
{
2007-10-26 08:13:29 +04:00
$this->_models = $models;
2007-09-20 19:02:57 +04:00
}
2007-09-20 19:02:57 +04:00
/**
* getModels
*
* Get the array of specified models to work with
*
* @return void
*/
public function getModels()
{
2007-10-26 08:13:29 +04:00
return $this->_models;
2007-09-20 19:02:57 +04:00
}
2007-09-20 19:02:57 +04:00
/**
2007-10-26 08:13:29 +04:00
* _exportIndividualFiles
2007-09-20 19:02:57 +04:00
*
* Set/Get whether or not to export individual files
*
2007-10-26 08:13:29 +04:00
* @return bool $_exportIndividualFiles
2007-09-20 19:02:57 +04:00
*/
public function exportIndividualFiles($bool = null)
{
if ($bool !== null) {
2007-10-26 08:13:29 +04:00
$this->_exportIndividualFiles = $bool;
2007-09-20 19:02:57 +04:00
}
2007-10-26 08:13:29 +04:00
return $this->_exportIndividualFiles;
2007-09-20 19:02:57 +04:00
}
2007-09-20 19:02:57 +04:00
/**
* exportData
*
* Interface for exporting data to fixtures files from Doctrine models
*
* @param string $directory
* @param string $format
* @param string $models
2007-10-26 08:13:29 +04:00
* @param string $_exportIndividualFiles
2007-09-20 19:02:57 +04:00
* @return void
*/
2007-10-26 08:13:29 +04:00
public function exportData($directory, $format = 'yml', $models = array(), $_exportIndividualFiles = false)
2007-09-20 19:02:57 +04:00
{
$export = new Doctrine_Data_Export($directory);
$export->setFormat($format);
$export->setModels($models);
2007-10-26 08:13:29 +04:00
$export->exportIndividualFiles($_exportIndividualFiles);
2007-09-20 19:02:57 +04:00
return $export->doExport();
}
2007-09-20 19:02:57 +04:00
/**
* importData
*
* Interface for importing data from fixture files to Doctrine models
*
* @param string $directory
* @param string $format
* @param string $models
* @return void
*/
public function importData($directory, $format = 'yml', $models = array())
2007-09-20 19:02:57 +04:00
{
$import = new Doctrine_Data_Import($directory);
$import->setFormat($format);
$import->setModels($models);
return $import->doImport();
}
2007-09-20 19:02:57 +04:00
/**
* importDummyData
*
* Interface for importing dummy data to models
*
* @param string $num
* @param string $models
* @return void
*/
public function importDummyData($num = 3, $models = array())
2007-09-20 19:02:57 +04:00
{
$import = new Doctrine_Data_Import();
$import->setModels($models);
2007-09-20 23:31:04 +04:00
return $import->doImportDummyData($num);
2007-09-20 19:02:57 +04:00
}
2007-09-20 19:02:57 +04:00
/**
* isRelation
*
* Check if a fieldName on a Doctrine_Record is a relation, if it is we return that relationData
*
* @param string $Doctrine_Record
* @param string $fieldName
* @return void
*/
public function isRelation(Doctrine_Record $record, $fieldName)
{
$relations = $record->getTable()->getRelations();
foreach ($relations as $relation) {
$relationData = $relation->toArray();
if ($relationData['local'] === $fieldName) {
return $relationData;
}
}
return false;
}
2007-10-20 06:30:15 +04:00
/**
* purge
*
* Purge all data for loaded models or for the passed array of Doctrine_Records
*
* @param string $models
* @return void
*/
public function purge($models = array())
{
$models = Doctrine::filterInvalidModels($models);
2007-10-20 06:30:15 +04:00
foreach ($models as $model)
{
$model = new $model();
$model->getTable()->createQuery()->delete($model)->execute();
}
2007-10-20 06:30:15 +04:00
}
2007-09-20 19:02:57 +04:00
}