Jump to content

PW 3.0.8 import pages with API, Integrity constraint violation: 1062 Duplicate entry


SiNNuT
 Share

Recommended Posts

Because i could not get ImportPagesCSV to work on 3.0.8 i decided to try a snippet that worked in the past. The following code resides in a template file. I tried with and without the namespace.

<?php namespace ProcessWire;

$template = wire('templates')->get("category");
$parent = wire('pages')->get("/categories/");
$numImported = 0;

if (($handle = fopen("./categories.tsv", "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 0, "\t")) !== FALSE) {
    // create the page and set template and parent
    $np = new Page();
    $np->template = $template;
    $np->parent = $parent;

    // set the new page fields from the CSV
    list($nptitle) = $data;
    $np->title = $nptitle;

    // set the URL name automatically
    $np->name = $nptitle;

    // save the new page
    $np->save();
    $numImported++;

    echo "Created page: {$np->url}\n";
  }
  echo $numImported;
}

But no matter what i do i get a fatal error because of Integrity constraint violation: 1062 Duplicate entry:

Fatal error: Exception: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'specials-1052' for key 'name_parent_id' (in C:\UniServerZ\vhosts\thvdw\wire\core\WireDatabasePDO.php line 233)

#0 C:\UniServerZ\vhosts\thvdw\wire\core\WireDatabasePDO.php(233): PDOStatement->execute()
#1 C:\UniServerZ\vhosts\thvdw\wire\core\PagesEditor.php(573): ProcessWire\WireDatabasePDO->execute(Object(PDOStatement))
#2 C:\UniServerZ\vhosts\thvdw\wire\core\PagesEditor.php(467): ProcessWire\PagesEditor->savePageQuery(Object(ProcessWire\Page), Array)
#3 C:\UniServerZ\vhosts\thvdw\wire\core\Pages.php(237): ProcessWire\PagesEditor->save(Object(ProcessWire\Page), Array)
#4 [internal function]: ProcessWire\Pages->___save(Object(ProcessWire\Page), Array)
#5 C:\UniServerZ\vhosts\thvdw\wire\core\Wire.php(303): call_user_func_array(Array, Array)
#6 C:\UniServerZ\vhosts\thvdw\wire\core\WireHooks.php(555): ProcessWire\Wire->_callMethod('___save', Array)
#7 C:\UniServerZ\vhosts\thvdw\wire\core\Wire.php(327): ProcessWire\WireHooks->runHoo in C:\UniServerZ\vhosts\thvdw\index.php on line 64

Thing is that despite the fatal error the pages do get created. I'm using very simple testdata. Just echoing out the tsv values works fine.

categories.tsv:

specials
beefburgers
lamb burgers
veggie burgers
burger complete
on the side
dessert
cold drinks
hot drinks

Anyone see any obvious mistakes in my code?

Link to comment
Share on other sites

Thanks Willy,

That took care of the fatal error. However i seem to be having a bad day doing simple stuff. Instead of just 9 pages it creates 18 pages, 9 without ending digit and then the same 9 with an ending digit. Somehow the file is being processed twice, or pages being created with a couple seconds in between? I checked and double checked that i only visited the page on the front end once...

2016-02-14%2023_21_47-localhost%20_%20Un

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...