User

Chris Davenport/JData/Adapters/CSV

From Joomla! Documentation

< User:Chris Davenport‎ | JData

JDataAdapterCsv is a JData adapter that can read and write data in comma-separated (CSV) format.

$config = array(
  'mode' => 'input',
  'ignorelines' => 0,
);
$adapter = JDataAdapter::getInstance( 'csv', 'JDataAdapter', $config );

Configuration[edit]

The following configuration variables are available:-

Key Value Default
mode is either "input" or "output" depending on whether the adapter is reading or writing CSV data respectively. input
ignorelines is an integer that is only valid if mode=input and indicats the number of leading lines in the data that are to be ignored. 0

Examples[edit]

Example 1: Processing CSV input data[edit]

It is often the case that the first line of a CSV file comprises the names of the fields used in the file. By default, JDataAdapterCsv will read the first line of the data and assume that this is the case and automatically set up callbacks for these field names.

For example, suppose you have the following CSV data in file "testfile.csv":

"Key","Title","Description"
"1","First","The first one"
"2","Second","The second one"

Then you could use code like this

$inputAdapter = JDataAdapter::getInstance( 'csv', 'JDataAdapter', array() );
$inputConnector = JDataConnector::getIntance( 'file', 'JDataConnector', array( 'file' => 'testfile.csv' ) );
$handler = JDataHandler::getInstance( 'csv', 'test' )
  ->attach( $inputAdapter, $inputConnector )
  ->attach( $outputAdapter, $outputConnector )
  ->execute()
  ;

If your handler extension class comprises

class TestCsv extends JDataHandlerSimple
{
    public function processFieldStartKey( JDataAdapter $field )
    {
        $this->addField( $field );
        return false;
    }

    public function processFieldStartTitle( JDataAdapter $field )
    {
        $this->addField( $field );
        return false;
    }

    public function processFieldStartDescription( JDataAdapter $field )
    {
        $this->addField( $field );
        return false;
    }
}

then all fields in the CSV would be parsed into data records as expected.

Note that you must use a connector in the handler attach method call. The JDataConnectorFile connector is used in the example above.

If the first line of the CSV does not comprise the field names then JDataAdapterCsv will assume that the fields are named "Csv1", "Csv2" and so on, and will call methods named "processFieldStartCsv1", and so on.

If the field names in the first line of the CSV are not appropriate (for example, if they cannot be resolved into valid PHP method names), then you can use the "ignorelines" configuration variable to ignore the field names and assume the default field names instead.

Example 2: Output data in CSV format[edit]

JDataAdapterCsv will always output the field names in the first line of the CSV data.

$outputAdapter = JDataAdapter::getInstance( 'csv', 'JDataAdapter', array( 'mode' => 'output' ) );
$outputConnector = JDataConnector::getIntance( 'echo', 'JDataConnector', array() );
$handler = JDataHandler::getInstance( 'csv', 'test' )
  ->attach( $inputAdapter, $inputConnector )
  ->attach( $outputAdapter, $outputConnector )
  ->execute()
  ;

Note that you must use a connector in the handler attach method call. The JDataConnectorEcho connector is used in the example above.