0

I am importing some nodes from csv file in drupal.

I am assigning row number to one of the field of that content type.

The first row in the csv is the header which contains the column names only. The data start from the second row.

I am using the "csvrownum" for the csv row number. But it starts from the first row. It should exclude the first row as the data starts from the second row. How will I do this?

Here is my code:

$this->source = new MigrateSourceCSV($path, $columns, array('header_rows' => 1));
          //The destination CCK (boundle)
          $node_options = MigrateDestinationNode::options("en", "full_html");
          $this->destination = new MigrateDestinationNode('lot', $node_options);
          $this->map = new MigrateSQLMap(
              $this->machineName, array(
            "csvrownum" => array(
              'type' => 'int',
              'unsigned' => TRUE,
              'not null' => TRUE,
              'alias' => 'import'
            )
              ), MigrateDestinationNode::getKeySchema()
          );
          //Field mapping
          $this->addFieldMapping('title', 'Title');
          $this->addFieldMapping('field_number', 'csvrownum');
2
  • If any of the answers below worked for you, please flag them as such... Commented Jun 18, 2014 at 22:54
  • Could you elaborate on why you've unaccepted the answer? provide more information and so on.
    – pcambra
    Commented Jul 1, 2014 at 14:01

4 Answers 4

1

I think that's the expected behavior, you could use the prepareRow() method to increment csvrownum in whatever your header_rows is.

0

Why not use the feeds module?

By default the parser is set to read Row 1. as a header row (although this can be turned off in the options) at the very least the code in 'FeedsCSVParser.inc' file will do something very similar to what you want and would be a good reference...cf. https://drupal.org/node/776854

0

What worked for me recently is defining the $columns array and passing it to new MigrateSourceCSV.

You may also need to use single-quotes instead of double-quotes (inconsistent above).

0

In order to skip first n lines you need to specify the 'header_rows' option.

e.g:

$options = array(
  'header_rows' => 1,
  'delimiter' => ','
);

$csvcolumns = array(
  0 => array('title_nl', 'Title NL'),
  1 => array('title_en', 'Title EN'),
  2 => array('title_fr', 'Title FR'),
);

$this->source = new MigrateSourceCSV("/../filename.csv", $csvcolumns, $options);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.