Doctrine 1.2.4
Doctrine_Data_Import Class Reference

Inherits Doctrine_Data.

Public Member Functions

 __construct ($directory=null)
 
 doImport ($append=false)
 
 doParsing ()
 
 exportData ($directory, $format= 'yml', $models=array(), $_exportIndividualFiles=false)
 
 exportIndividualFiles ($bool=null)
 
 getDirectory ()
 
 getFormat ()
 
 getFormats ()
 
 getModels ()
 
 importData ($directory, $format= 'yml', $models=array(), $append=false)
 
 isRelation (Doctrine_Record $record, $fieldName)
 
 purge ($models=null)
 
 setDirectory ($directory)
 
 setFormat ($format)
 
 setModels ($models)
 

Protected Member Functions

 _buildNestedSetRows ($className, $data)
 
 _buildRows ($className, $data)
 
 _getImportedObject ($rowKey, Doctrine_Record $record, $relationName, $referringRowKey)
 
 _getRowKeyPrefix (Doctrine_Table $table)
 
 _hasNaturalNestedSetFormat ($className, array &$data)
 
 _loadData (array $array)
 
 _loadNestedSetData ($model, $nestedSetData, $parent=null)
 
 _processRow ($rowKey, $row)
 

Detailed Description

Definition at line 33 of file Import.php.

Constructor & Destructor Documentation

Doctrine_Data_Import::__construct (   $directory = null)

Optionally pass the directory/path to the yaml for importing

Parameters
string$directory
Returns
void

Definition at line 55 of file Import.php.

{
if ($directory !== null) {
$this->setDirectory($directory);
}
}

Member Function Documentation

Doctrine_Data_Import::_buildNestedSetRows (   $className,
  $data 
)
protected

Build the rows for nested set models

Returns
void

Definition at line 162 of file Import.php.

{
foreach ($data as $rowKey => $row) {
$children = isset($row['children']) ? $row['children']:array();
unset($row['children']);
$this->_rows[$className][$rowKey] = $row;
$this->_buildNestedSetRows($className, $children);
}
}
Doctrine_Data_Import::_buildRows (   $className,
  $data 
)
protected

Recursively loop over all data fixtures and build the array of className rows

Returns
void

Definition at line 126 of file Import.php.

{
$table = Doctrine_Core::getTable($className);
foreach ($data as $rowKey => $row) {
// do the same for the row information
$this->_rows[$className][$rowKey] = $row;
foreach ((array) $row as $key => $value) {
if ($table->hasRelation($key) && is_array($value) && ! $table->hasTemplate('Doctrine_Template_I18n')) {
// Skip associative arrays defining keys to relationships
if ( ! isset($value[0]) || (isset($value[0]) && is_array($value[0]))) {
$rel = $table->getRelation($key);
$relClassName = $rel->getTable()->getOption('name');
$relRowKey = $rowKey . '_' . $relClassName;
if ($rel->getType() == Doctrine_Relation::ONE) {
$val = array($relRowKey => $value);
$this->_rows[$className][$rowKey][$key] = $relRowKey;
} else {
$val = $value;
$this->_rows[$className][$rowKey][$key] = array_keys($val);
}
$this->_buildRows($relClassName, $val);
}
}
}
}
}
Doctrine_Data_Import::_getImportedObject (   $rowKey,
Doctrine_Record  $record,
  $relationName,
  $referringRowKey 
)
protected

Get the unsaved object for a specified row key and validate that it is the valid object class for the passed record and relation name

Parameters
string$rowKey
Doctrine_Record$record
string$relationName
string$referringRowKey
Returns
Doctrine_Record
Exceptions
Doctrine_Data_Exception

Definition at line 184 of file Import.php.

{
$relation = $record->getTable()->getRelation($relationName);
$rowKey = $this->_getRowKeyPrefix($relation->getTable()) . $rowKey;
if ( ! isset($this->_importedObjects[$rowKey])) {
sprintf('Invalid row key specified: %s, referred to in %s', $rowKey, $referringRowKey)
);
}
$relatedRowKeyObject = $this->_importedObjects[$rowKey];
$relationClass = $relation->getClass();
if ( ! $relatedRowKeyObject instanceof $relationClass) {
throw new Doctrine_Data_Exception(sprintf(
'Class referred to in "%s" is expected to be "%s" and "%s" was given',
$referringRowKey, $relation->getClass(), get_class($relatedRowKeyObject)
));
}
return $relatedRowKeyObject;
}
Doctrine_Data_Import::_getRowKeyPrefix ( Doctrine_Table  $table)
protected

Returns the prefix to use when indexing an object from the supplied table.

Parameters
Doctrine_Table$table
Returns
string

Definition at line 398 of file Import.php.

{
return sprintf('(%s) ', $table->getTableName());
}
Doctrine_Data_Import::_hasNaturalNestedSetFormat (   $className,
array &  $data 
)
protected

NestedSet fixtures may come in a 'natural' format with nested children listed under a 'children' key or in a raw, non-nested format with lft/rgt values.

This method returns true if the given $data is a nested set in 'natural' form.

Parameters
$className
$data
Returns
boolean

Definition at line 272 of file Import.php.

{
if (Doctrine_Core::getTable($className)->isTree()) {
if (isset($data['NestedSet']) && $data['NestedSet'] == true) {
unset($data['NestedSet']);
return true;
} else {
$first = current($data);
return array_key_exists('children', $first);
}
} else {
return false;
}
}
Doctrine_Data_Import::_loadData ( array  $array)
protected

Perform the loading of the data from the passed array

Parameters
string$array
Returns
void

Definition at line 293 of file Import.php.

{
$nestedSets = array();
$specifiedModels = $this->getModels();
$rows = array();
foreach ($array as $className => $data) {
if ( ! empty($specifiedModels) && !in_array($className, $specifiedModels)) {
continue;
}
// if loaded data is a nested set in natural format, process through _buildNestedSetRows.
// 'raw' nested sets and all other models are processed through _buildRows.
if ($this->_hasNaturalNestedSetFormat($className, $data)) {
$nestedSets[$className][] = $data;
$this->_buildNestedSetRows($className, $data);
} else {
$this->_buildRows($className, $data);
}
}
$buildRows = array();
foreach ($this->_rows as $className => $classRows) {
$rowKeyPrefix = $this->_getRowKeyPrefix(Doctrine_Core::getTable($className));
foreach ($classRows as $rowKey => $row) {
$rowKey = $rowKeyPrefix . $rowKey;
$buildRows[$rowKey] = $row;
$this->_importedObjects[$rowKey] = new $className();
$this->_importedObjects[$rowKey]->state('TDIRTY');
}
}
foreach($buildRows as $rowKey => $row) {
$this->_processRow($rowKey, $row);
}
// save natural nested set fixture data and unset from _importedObjects
foreach ($nestedSets as $className => $sets) {
foreach ($sets as $data) {
$this->_loadNestedSetData($className, $data);
}
}
foreach ($manager as $connection) {
$tree = $connection->unitOfWork->buildFlushTree(array_keys($array));
foreach ($tree as $model) {
foreach ($this->_importedObjects as $obj) {
if ($obj instanceof $model) {
$obj->save();
}
}
}
}
}
Doctrine_Data_Import::_loadNestedSetData (   $model,
  $nestedSetData,
  $parent = null 
)
protected

Load nested set data for models with nested set enabled

Parameters
string$model
string$nestedSetData
string$parent
Returns
void

Definition at line 361 of file Import.php.

{
foreach($nestedSetData AS $rowKey => $nestedSet) {
$children = array();
$data = array();
if (array_key_exists('children', $nestedSet)) {
$children = (array) $nestedSet['children'];
$children = array_reverse($children, true);
unset($nestedSet['children']);
}
$rowKey = $this->_getRowKeyPrefix(Doctrine_Core::getTable($model)) . $rowKey;
$record = $this->_importedObjects[$rowKey];
// remove this nested set from _importedObjects so it's not processed in the save routine for normal objects
unset($this->_importedObjects[$rowKey]);
if ( ! $parent) {
$record->save(); // save, so that createRoot can do: root id = id
Doctrine_Core::getTable($model)->getTree()->createRoot($record);
} else {
$parent->getNode()->addChild($record);
}
if (is_array($children) AND !empty($children)) {
$this->_loadNestedSetData($model, $children, $record);
}
}
}
Doctrine_Data_Import::_processRow (   $rowKey,
  $row 
)
protected

Process a row and make all the appropriate relations between the imported data

Parameters
string$rowKey
string$row
Returns
void

Definition at line 215 of file Import.php.

{
$obj = $this->_importedObjects[$rowKey];
foreach ((array) $row as $key => $value) {
if (method_exists($obj, 'set' . Doctrine_Inflector::classify($key))) {
$func = 'set' . Doctrine_Inflector::classify($key);
$obj->$func($value);
} else if ($obj->getTable()->hasField($key)) {
if ($obj->getTable()->getTypeOf($key) == 'object') {
$value = unserialize($value);
}
$obj->set($key, $value);
} else if ($obj->getTable()->hasRelation($key)) {
if (is_array($value)) {
if (isset($value[0]) && ! is_array($value[0])) {
foreach ($value as $link) {
if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::ONE) {
$obj->set($key, $this->_getImportedObject($link, $obj, $key, $rowKey));
} else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) {
$relation = $obj->$key;
$relation[] = $this->_getImportedObject($link, $obj, $key, $rowKey);
}
}
} else {
$obj->$key->fromArray($value);
}
} else {
$obj->set($key, $this->_getImportedObject($value, $obj, $key, $rowKey));
}
} else {
try {
$obj->$key = $value;
} catch (Exception $e) {
// used for Doctrine plugin methods (Doctrine_Template)
if (is_callable(array($obj, 'set' . Doctrine_Inflector::classify($key)))) {
$func = 'set' . Doctrine_Inflector::classify($key);
$obj->$func($value);
} else {
throw new Doctrine_Data_Exception('Invalid fixture element "'. $key . '" under "' . $rowKey . '"');
}
}
}
}
}
Doctrine_Data_Import::doImport (   $append = false)

Do the importing of the data parsed from the fixtures

Returns
void

Definition at line 110 of file Import.php.

{
$array = $this->doParsing();
if ( ! $append) {
$this->purge(array_reverse(array_keys($array)));
}
$this->_loadData($array);
}
Doctrine_Data_Import::doParsing ( )

Do the parsing of the yaml files and return the final parsed array

Returns
array $array

Definition at line 67 of file Import.php.

{
$recursiveMerge = Doctrine_Manager::getInstance()->getAttribute(Doctrine_Core::ATTR_RECURSIVE_MERGE_FIXTURES);
$mergeFunction = $recursiveMerge === true ? 'array_merge_recursive':'array_merge';
$directory = $this->getDirectory();
$array = array();
if ($directory !== null) {
foreach ((array) $directory as $dir) {
$e = explode('.', $dir);
// If they specified a specific yml file
if (end($e) == 'yml') {
$array = $mergeFunction($array, Doctrine_Parser::load($dir, $this->getFormat()));
// If they specified a directory
} else if (is_dir($dir)) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY);
$filesOrdered = array();
foreach ($it as $file) {
$filesOrdered[] = $file;
}
// force correct order
natcasesort($filesOrdered);
foreach ($filesOrdered as $file) {
$e = explode('.', $file->getFileName());
if (in_array(end($e), $this->getFormats())) {
$array = $mergeFunction($array, Doctrine_Parser::load($file->getPathName(), $this->getFormat()));
}
}
}
}
}
return $array;
}
Doctrine_Data::exportData (   $directory,
  $format = 'yml',
  $models = array(),
  $_exportIndividualFiles = false 
)
inherited

exportData

Interface for exporting data to fixtures files from Doctrine models

Parameters
string$directory
string$format
string$models
string$_exportIndividualFiles
Returns
void

Definition at line 196 of file Data.php.

{
$export = new Doctrine_Data_Export($directory);
$export->setFormat($format);
$export->setModels($models);
$export->exportIndividualFiles($_exportIndividualFiles);
return $export->doExport();
}
Doctrine_Data::exportIndividualFiles (   $bool = null)
inherited

_exportIndividualFiles

Set/Get whether or not to export individual files

Returns
bool $_exportIndividualFiles

Definition at line 176 of file Data.php.

{
if ($bool !== null) {
$this->_exportIndividualFiles = $bool;
}
return $this->_exportIndividualFiles;
}
Doctrine_Data::getDirectory ( )
inherited

getDirectory

Get directory for dumping/loading data from and to

Returns
void

Definition at line 139 of file Data.php.

{
return $this->_directory;
}
Doctrine_Data::getFormat ( )
inherited

getFormat

Get the current format we are working with

Returns
void

Definition at line 103 of file Data.php.

{
return $this->_format;
}
Doctrine_Data::getFormats ( )
inherited

getFormats

Get array of available formats

Returns
void

Definition at line 115 of file Data.php.

{
return $this->_formats;
}
Doctrine_Data::getModels ( )
inherited

getModels

Get the array of specified models to work with

Returns
void

Definition at line 164 of file Data.php.

{
return $this->_models;
}
Doctrine_Data::importData (   $directory,
  $format = 'yml',
  $models = array(),
  $append = false 
)
inherited

importData

Interface for importing data from fixture files to Doctrine models

Parameters
string$directory
string$format
string$models
Returns
void

Definition at line 216 of file Data.php.

{
$import = new Doctrine_Data_Import($directory);
$import->setFormat($format);
$import->setModels($models);
return $import->doImport($append);
}
Doctrine_Data::isRelation ( Doctrine_Record  $record,
  $fieldName 
)
inherited

isRelation

Check if a fieldName on a Doctrine_Record is a relation, if it is we return that relationData

Parameters
string$Doctrine_Record
string$fieldName
Returns
void

Definition at line 234 of file Data.php.

{
$relations = $record->getTable()->getRelations();
foreach ($relations as $relation) {
$relationData = $relation->toArray();
if ($relationData['local'] === $fieldName) {
return $relationData;
}
}
return false;
}
Doctrine_Data::purge (   $models = null)
inherited

purge

Purge all data for loaded models or for the passed array of Doctrine_Records

Parameters
string$models
Returns
void

Definition at line 258 of file Data.php.

{
if ($models) {
} else {
}
$connections = array();
foreach ($models as $model) {
$connections[Doctrine_Core::getTable($model)->getConnection()->getName()][] = $model;
}
foreach ($connections as $connection => $models) {
$models = Doctrine_Manager::getInstance()->getConnection($connection)->unitOfWork->buildFlushTree($models);
$models = array_reverse($models);
foreach ($models as $model) {
Doctrine_Core::getTable($model)->createQuery()->delete()->execute();
}
}
}
Doctrine_Data::setDirectory (   $directory)
inherited

setDirectory

Set the array/string of directories or yml file paths

Returns
void

Definition at line 127 of file Data.php.

{
$this->_directory = $directory;
}
Doctrine_Data::setFormat (   $format)
inherited

setFormat

Set the current format we are working with

Parameters
string$format
Returns
void

Definition at line 91 of file Data.php.

{
$this->_format = $format;
}
Doctrine_Data::setModels (   $models)
inherited

setModels

Set the array of specified models to work with

Parameters
string$models
Returns
void

Definition at line 152 of file Data.php.

{
$this->_models = $models;
}

The documentation for this class was generated from the following file: