0

Problem

I'm trying to create multiple nodes at once using a CSV file for the data. However my script only creates the first 25 before it throws the following error. Any ideas what might be happening?

PDOException: in drupal_write_record() (line 7166 of C:\XAMPP\htdocs\drupalie\includes\common.inc).

Set-Up

I have a custom form which creates the nodes on submit. The form just has a submit button.

This is the submit handler. I only have one column in my CSV sheet so I don't actually use the delimiter. First I generate an array with each node name.

  if (($handle = fopen("sites/all/modules/custom/generatenodes/nodes_database_import.csv", "r")) !== FALSE) {
    //open the CSV file
    $length = 1000;
    $delimiter = "|";
    //set the length and delimiter.
    while (!feof($handle)) {
      //$line is an array of the csv elements
      $node_name_array[] = fgetcsv($handle, $length, $delimiter);
    }

    fclose($handle);
  }

The array looks like this:enter image description here

I then create the nodes:

global $user;
foreach ($node_name_array as $nodename) {
  if($nodename == FALSE) {
    return;
  }

    $values = array(
      'type' => 'school',
      'uid' => $user->uid,
      'status' => 1,
      'comment' => 0,
      'promote' => 0,
    );
    $entity = entity_create('node', $values);

    $ewrapper = entity_metadata_wrapper('node', $entity);

    $ewrapper->title->set($nodename['0']);
    $ewrapper->field_country->set($schoolname['0']);
     $ewrapper->save(true);
    entity_save('node', $entity);
}

But it breaks after the first 25 or so. What have I missed?

EDIT--

So I found the cause. It's to do with the values in my CSV file. Sometimes it likes them othertimes it doesn't. If I replace these values then it works up to the next broken value.

The first problem was a value with an accented e inside. Removing the accent fixed it.

Unfortunately the next two which broke it are not so obvious:

  • Putian Cambridge English
  • Zhangzhou Bilingual Middle School

Yet the following values work:

  • Quanzhou Foreign Language Middle School
  • International College of Xiamen University

I've only pasted in clean values, but something is breaking it. Any ideas?

Trying to navigate to the content page after causes MySQL to break. Here's the error log:

https://www.dropbox.com/s/jji3cw559filq03/mysqlcrash_log.txt

bumped to the homepage by Community 2 days ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • Are you sure it's the whole message? No exception name, number, message, nothing? Nothing in db_log or server's error.log? – Mołot Oct 28 '13 at 12:53
  • @Molot I don't think so. The last errors are from two hours ago so unless it updates in periodic chunks and the errors haven't appeared there is nothing in apache or mysql logs. – Dominic Woodman Oct 28 '13 at 13:08
  • That's weird, usually when you get PDO exception, it carry underlying MySQL / PostgreSQL error message. If it does not, it'll be hard to tell what exactly is wrong. – Mołot Oct 28 '13 at 13:11
  • 1
    @Molot So after some digging I found it's the values in the CSV file which break it. The first was an accented e, but I can't spot a pattern otherwise. – Dominic Woodman Oct 28 '13 at 13:34
  • @Molot MySQL does now crash and create an error log, but only when I go to the content page. Added to the post. – Dominic Woodman Oct 28 '13 at 13:53
0

You should be using migrate for this: https://www.drupal.org/project/migrate, it covers many of the pitfalls you are currently encountering when creating or importing content from a CSV, database or XML file. Don't recreate the wheel when someone has already created it for you.

It's not like the feeds module or other "import" type modules, it's an entirely code based OOP solution. You just extend the main migration class and map your data. It comes with a number of built in PHP functions to process the data further and it literally covers almost every scenario you could possibly encounter when mass creating or transferring content.

As someone who's been down this path many times, it will save you a lot of time and hair pulling. I recommend looking at the code examples migrate provides.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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