Comments: on this page. Click to read or post your own.

Class BulkLoader

Description

A base for bulk loaders of content into the SilverStripe database.

Bulk loaders give SilverStripe authors the ability to do large-scale uploads into their Sapphire databases.

You can configure column-handling,

  • author: Ingo Schommer, Silverstripe Ltd. (<firstname>@silverstripe.com)
  • see: http://rfc.net/rfc4180.html
  • todo: Add support for adding/editing has_many relations.
  • todo: Character conversion
  • todo: Add support for deep chaining of relation properties (e.g. Player.Team.Stats.GoalCount)
  • abstract:

Located in /sapphire/dev/BulkLoader.php (line 17)

Object
   |
   --ViewableData
      |
      --BulkLoader
Direct descendents
Class Description
CsvBulkLoader Uses the fgetcsv() function to process CSV input.
Variable Summary
static string $title
array $columnMap
string $objectClass
Method Summary
BulkLoader __construct ( $objectClass)
array getImportSpec ()
void getOptionFields ()
boolean isNullValue (mixed $val, [ $fieldName = null], string $field)
void load ( $filepath)
array preview (string $filepath)
BulkLoader_Result processAll (string $filepath, [boolean $preview = false])
void processRecord (array $record, array $columnMap, $result &$result, [boolean $preview = false])
string Title ()
Variables
static string $title (line 33)

Override this on subclasses to give the specific functions names.

  • access: public
array $columnMap = array() (line 72)

Map columns to DataObject-properties.

If not specified, we assume the first row in the file contains the column headers. The order of your array should match the column order.

The column count should match the count of array elements, fill with NULL values if you want to skip certain columns.

You can also combine $hasHeaderRow = true and $columnMap and omit the NULL values in your map.

Supports one-level chaining of has_one relations and properties with dot notation (e.g. Team.Title). The first part has to match a has_one relation name (not necessarily the classname of the used relation).

  1.  <?php
  2.      // simple example
  3.   array(
  4.       'Title',
  5.          'Birthday'
  6.      )
  7.  
  8.  // complex example
  9.      array(
  10.          'first name' => 'FirstName'// custom column name
  11.          null// ignored column
  12.          'RegionID'// direct has_one/has_many ID setting
  13.          'OrganisationTitle'// create has_one relation to existing record using $relationCallbacks
  14.          'street' => 'Organisation.StreetName'// match an existing has_one or create one and write property.
  15.      );
  16.  ?>

  • access: public
array $duplicateChecks = array() (line 112)

Specifies how to determine duplicates based on one or more provided fields in the imported data, matching to properties on the used DataObject class.

Alternatively the array values can contain a callback method (see example for implementation details). If multiple checks are specified, the first one "wins".

  1.  <?php
  2.  array(
  3.          'customernumber' => 'ID',
  4.          'phonenumber' => array(
  5.              'callback' => 'getByImportedPhoneNumber'
  6.          )
  7.  );
  8.  ?>

  • access: public
string $objectClass (line 26)

Each row in the imported dataset should map to one instance of this class (with optional property translation through {@self::$columnMaps}.

  • access: public
array $relationCallbacks = array() (line 90)

Find a has_one relation based on a specific column value.

  1.  <?php
  2.  array(
  3.          'OrganisationTitle' => array(
  4.              'relationname' => 'Organisation'// relation accessor name
  5.              'callback' => 'getOrganisationByTitle',
  6.         );
  7.  );
  8.  ?>

  • access: public

Inherited Variables

Inherited from ViewableData

ViewableData::$casting
ViewableData::$castingHelperPair_cache
ViewableData::$customisedObj
ViewableData::$failover
ViewableData::$iteratorPos
ViewableData::$iteratorTotalItems
ViewableData::$namedAs
ViewableData::$parent
ViewableData::$_natural_cache
ViewableData::$_object_cache
ViewableData::$_xml_cache

Inherited from Object

Object::$builtInMethods
Object::$class
Object::$classConstructed
Object::$extensions
Object::$extension_instances
Object::$extraMethods
Object::$extraStatics
Object::$statics
Object::$static_cached
Methods
Constructor __construct (line 114)
BulkLoader __construct ( $objectClass)
  • $objectClass

Redefinition of:
Object::__construct()
getImportSpec (line 198)

Get a specification of all available columns and relations on the used model.

Useful for generation of spec documents for technical end users.

Return Format: <example> array( 'fields' => array('myFieldName'=>'myDescription'), 'relations' => array('myRelationName'=>'myDescription'), ) </example>

  • todo: Mix in custom column mappings
  • access: public
array getImportSpec ()
getOptionFields (line 169)

Return a FieldSet containing all the options for this form; this

doesn't include the actual upload field itself

  • access: public
void getOptionFields ()
isNullValue (line 224)

Determines if a specific field is null.

Can be useful for unusual "empty" flags in the file, e.g. a "(not set)" value. The usual DBField::isNull() checks apply when writing the DataObject, so this is mainly a customization method.

  • access: protected
boolean isNullValue (mixed $val, [ $fieldName = null], string $field)
  • mixed $val
  • string $field: Name of the field as specified in the array-values for self::$columnMap.
  • $fieldName
load (line 124)
  • access: public
void load ( $filepath)
  • $filepath
preview (line 140)

Preview a file import (don't write anything to the database).

Useful to analyze the input and give the users a chance to influence it through a UI.

  • return: See self::processAll()
  • todo: Implement preview()
  • access: public
array preview (string $filepath)
  • string $filepath: Absolute path to the file we're importing
processAll (line 152)

Process every record in the file

  • return: A collection of objects which are either created, updated or deleted. 'message': free-text string that can optionally provide some more information about what changes have
  • abstract:
  • access: protected
BulkLoader_Result processAll (string $filepath, [boolean $preview = false])
  • string $filepath: Absolute path to the file we're importing (with UTF8 content)
  • boolean $preview: If true, we'll just output a summary of changes but not actually do anything

Redefined in descendants as:
processRecord (line 163)

Process a single record from the file.

  • abstract:
  • access: protected
void processRecord (array $record, array $columnMap, $result &$result, [boolean $preview = false])
  • array $record: An map of the data, keyed by the header field defined in self::$columnMap
  • array $columnMap
  • boolean $preview
  • $result &$result: BulkLoader_Result (passed as reference)

Redefined in descendants as:
Title (line 177)

Return a human-readable name for this object.

It defaults to the class name can be overridden by setting the static variable $title

  • access: public
string Title ()

Inherited Methods

Inherited From ViewableData

ViewableData::ATT_val()
ViewableData::BaseHref()
ViewableData::buildCastingHelperCache()
ViewableData::cachedCall()
ViewableData::castingHelper()
ViewableData::castingHelperPair()
ViewableData::castingObjectCreator()
ViewableData::castingObjectCreatorPair()
ViewableData::CSSClasses()
ViewableData::CurrentMember()
ViewableData::CurrentPage()
ViewableData::customise()
ViewableData::Debug()
ViewableData::defineMethods()
ViewableData::escapeTypeForField()
ViewableData::Even()
ViewableData::EvenOdd()
ViewableData::First()
ViewableData::FirstLast()
ViewableData::getField()
ViewableData::getIterator()
ViewableData::getXMLValues()
ViewableData::hasField()
ViewableData::HasPerm()
ViewableData::hasValue()
ViewableData::i18nLocale()
ViewableData::IsAjax()
ViewableData::iteratorProperties()
ViewableData::JS_val()
ViewableData::Last()
ViewableData::Me()
ViewableData::Middle()
ViewableData::MiddleString()
ViewableData::obj()
ViewableData::Odd()
ViewableData::Pos()
ViewableData::RAW_val()
ViewableData::renderWith()
ViewableData::SecurityID()
ViewableData::setCustomisedObj()
ViewableData::setField()
ViewableData::SQL_val()
ViewableData::ThemeDir()
ViewableData::Top()
ViewableData::TotalItems()
ViewableData::val()
ViewableData::XML_val()
ViewableData::__get()
ViewableData::__isset()
ViewableData::__set()

Inherited From Object

Object::__construct()
Object::addMethodsFrom()
Object::addStaticVars()
Object::addWrapperMethod()
Object::add_extension()
Object::allMethodNames()
Object::buildMethodList()
Object::cacheToFile()
Object::cacheToFileWithArgs()
Object::create()
Object::createMethod()
Object::defineMethods()
Object::exists()
Object::extend()
Object::extInstance()
Object::getCustomClass()
Object::hasExtension()
Object::hasMethod()
Object::invokeWithExtensions()
Object::is_a()
Object::loadCache()
Object::parentClass()
Object::sanitiseCachename()
Object::saveCache()
Object::set_stat()
Object::set_uninherited()
Object::stat()
Object::strong_create()
Object::uninherited()
Object::useCustomClass()
Object::__call()
Object::__toString()
blog comments powered by Disqus

Documentation generated on Sun, 19 Oct 2008 06:38:31 +1300 by phpDocumentor 1.3.2