Jump to content
neosin

[solved] issue importing data with API

Recommended Posts

Hi all

I am bootstraping PW to import large batches of data however I am having a hard time importing the alternate language using the suggested method

I am using the language alternate field method as outlined here https://processwire.com/api/multi-language-support/multi-language-fields/#language-alternate-field-values

here is the code  I am using and the issue is that the French fields do not populate at all. The English data is fine but for some reason the French does not get filled in.

The name of the alt lang is "fr" and the title is "Français", so I assumed I could do $page->body_fr but this seems not to be the case?

while($row = $result->fetch_assoc()) {	  
	  $p = new Page();
	  $p->template = "video-item";
	  $p->parent_id = 1057;
	  $p->of(false); // outputFormatting must be OFF
	  $p->title = $row['title_en'];
	  $p->title_fr = $row['title_fr'];
	  $p->summary = $row['description_en'];
	  $p->summary_fr = $row['description_fr'];
	  $p->save();
}

The external database (non-PW) fields for French all exist and all have data.

It seems if I have outputFormatting on or off makes no difference, initially I did not have it but after reading the link above I added it but it has no effect so far.

What am I doing wrong?

Share this post


Link to post
Share on other sites

Your code snippet is inside a foreach, right? You have to save the Page at the end.

$p->save();

 

Share this post


Link to post
Share on other sites

yes sorry I didn't include that bit, but yes I have that, since the English saves.

I've updated the OP code to show the full thing

Share this post


Link to post
Share on other sites

Hm it's strange. Can you try this?

$frz = $languages->get("fr");
$p->of(false); 
$p->summary->setLanguageValue($frz, $row['description_fr']);
$p->save();

 

Share this post


Link to post
Share on other sites

@maxf5 it gives error "Call to a member function setLanguageValue() on string"  but we are using $p->of(false); so it should be an array... I am confused

This is the update code which is giving this error now

while($row = $result->fetch_assoc()) {
	$fr = $languages->get("fr");  
	$p->of(false);

	$p->title = $row['title_en'];
	$p->title->setLanguageValue($fr, $row['title_fr']);
	$p->summary = $row['description_en'];
	$p->summary->setLanguageValue($fr, $row['description_fr']);
	$p->save();
}

 

Share this post


Link to post
Share on other sites
1 hour ago, neosin said:

It seems if I have outputFormatting on or off makes no difference,

It's always off when bootstrapping, so no need to set it yourself.

Share this post


Link to post
Share on other sites

Hmm I found this old post 

it seems that some language options like LanguageSupportPageNames does not get loaded when bootstrapping?

not sure if this is related to my issue

 

Share this post


Link to post
Share on other sites
2 minutes ago, bernhard said:

 


$p->setLanguageValue($language, $field, $value);

You cannot call it from the $p->summary property ;)


Doesn't it work when he prefix his file with:

<?php namespace ProcessWire;
  • Like 1

Share this post


Link to post
Share on other sites
7 minutes ago, bernhard said:

 

this worked

thank you very much!

$p->summary = $description_en;
$p->setLanguageValue($fr,'summary',$description_fr);

:)

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...