The __import__xxx() methods are intended for you to define how to import data in a specific format.
In the simplest example, suppose you want to be able to import a list of email addresses in a text file of the form:
Then we might write an import filter as follows:
- Code: Select all
function __import__emails($data){
// $data is a list of email addresses - one per line
// Split the addresses into an array.
$addresses = explode("\n", $data);
// Create an array to house our records for importing.
$records = array();
foreach ( $addresses as $addr ){
// Create record on the 'addresses' table
$record = new Dataface_Record('addresses', array());
$record->setValue('email', $addr);
$records[] = $record;
unset($record);
}
// Return the array of records that we will be importing.
return $records;
}
In the case of CSV files it's just a little more complicated because we need to parse the CSV format, rather than just taking one value per line as we did in this example. But we make use of PHP's fgetcsv() function to help us with this.