Jump to content

Does this image import pseudocode look OK?


MarcC
 Share

Recommended Posts

I have a big database full of various tables that I plan to import to ProcessWire as CSV. Each record/row has a field corresponding to an image number. Then I have a folder full of the numbered images. After importing the CSV, I am planning to import these image like so:

foreach $imported_pages as $page {
  if ($page->image_number) {
    if (file exists with image_number in the filename) {
      //Code below from Ryan
      $page->images->add("path or URL to image");
      //save the page
      $page->save();
    }
  }
}

Does that look right? Thanks!

  • Like 2
Link to comment
Share on other sites

$num = 0;
foreach $imported_pages as $page {
  if ($page->image_number) {
    if (file exists with image_number in the filename) {
      $num++;
      //Code below from Ryan
      $page->images->add("path or URL to image");
      //save the page
      $page->save();
      echo "added image with {$page->image_number} to {$page->name}\n";
    }
  }

  // added because you have said a "big database"
  // (it is picked up from Ryans code somewhere in forum examples)
  wire('pages')->uncache($page); // just in case we need the memory

  if($num >= 5) break;
}

Looks good. I'm actually working on a DB for a colleague and I have imported 2000 pages and 35.000 images similar like that.

For testing I use breaks in the loop to just test only one import, or use a counter and import 5 or 10 images before breaking.

When seeing it works well I comment the breaks.

Also I make a SQL-Dump before testing/modifying/importing.

And I write logfiles.

And I love importer-scripts! ^-^

  • Like 4
Link to comment
Share on other sites

Looks fine to me MarcC. My process here is pretty much identical to Horst's.  Over the next couple weeks, I have to import 3000+ pages each with 10-50 photos. When doing large quantities, you may need to paginate it, or run it multiple times, if you run into any memory issues. Check if the page you are creating already exists and skip it if it does. Something you may also need at the top of your PHP file is: 

ini_set('max_execution_time', 60 * 60); // give it an hour. :) 
  • Like 1
Link to comment
Share on other sites

Thanks everybody--I'll give it a shot on Monday and see how it goes. I just watched "Cloudy With a Chance of Meatballs" with my kids and now I'm dreading this job a bit--can't recommend watching that film before doing batch operations.  :-)

  • Like 1
Link to comment
Share on other sites

I just watched "Cloudy With a Chance of Meatballs" with my kids and now I'm dreading this job a bit--can't recommend watching that film before doing batch operations. :-)

Greetings,

Very funny! But I dare you to try coding while your kid listens to "Fox in Socks" (as I did this morning). You will temporarily abandon all hope of OO or MVC.

Back on topic: the importing ideas in this discussion are great!

Thanks,

Matthew

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...