Sign in to follow this  
cosmicsafari

Page save() method dying without output?

Recommended Posts

Hi all,

Im a bit confused by an issue I have come across today.

I have a module which connects to a third party (once an hour using LazyCron), parses a publicly available XML file, turns it into useable information which I then use the API to save as PW Pages.

On the whole this has been working great however today I noticed that it kept failing on one of the imports.

After doing some investigation I realised its appears to be dying at the save page stage.

From the documentation $page-save() should return either True/False, so I thought I would update the code to reflect this while debugging.

$this->log(1);
$bool = $p->save();
if($bool){
  $this->log('Saved successfully');
} else {
  $this->log('Fail to save');
}
$this->log(2);

However the script only gets to the save() part and then appears to terminate. Then when checking the error log the latest entry is always just '1'

Any ideas as i'm a tad confused why I at least don't get a response of some kind?

 

 

 

Share this post


Link to post
Share on other sites

Not sure, but I would first of all look at the XML file. Has anything changed recently? What happens if you "manually" grab the XML, and update/create page(s)?

  • Like 1

Share this post


Link to post
Share on other sites

Thanks for the reply but I think I have sussed it, not sure why it was never an issue up to this point right enough.

I updated my code from above to try:

$this->log(1);
try {
     $p->save();
} catch (\Exception $e) {
     $this->log($e);
}
$this->log(2);

Which gave me an error mentioning using $p->setOutputFormatting();

So I updated to

$this->log(1);
try {
     $p->setOutputFormatting(false);
     $p->save();
} catch (\Exception $e) {
     $this->log($e);
}
$this->log(2);

Which appears to have fixed the issue.

:)

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By louisstephens
      So this is more or less just a question regarding best practices, and how to actually achieve this. I have an internal tool I built awhile ago that has just gotten very "trashed". The naming conventions are all out of whack due to trying to get something completed, and pages are just everywhere. The fields got really out of hand as well, but I believe I have found a much cleaner field structure with the Repeater Matrix and a few other ProFields.  I decided recently to rewrite the template structure and the fields from scratch to keep everything nice and neat, but then it hit me that I would need to move all the previous data over and populate the new fields etc.
      The current tree looks something like:
      Group Title - Section A (template "section") - Category One (template "category") - Category Two (template "category") - Section B (template "section") - Category One (template "category") - Category Two (template "category") Etc etc... The structure is pretty good, and I am thinking of keeping it as it serves my purpose. However, I now have about 140 Sections with child pages that I would hopefully like to move to different templates, as well as changing the child templates/mapping data to the new fields. The only fields I need to worry about are a few textareas, text fields, options field,  as well as 1 image filed.
      I guess my real question is, what is the best way to go about this "migration" via the api while keeping the parent/child structure. It would be ok to leave it as is and chip away at it if I were the only one using it, but unfortunately that is not the case. 
      I guess one approach would be to copy over the entire structure and make the move to the new templates/fields. Once we have verified all the data/templates are correct, we could remove the old structure and possibly rename the new to mirror the old. However, this might just be too complicated.
    • By sam-vital
      Hi,
      So I'm working on my site, getting round to completing it. I remember that I haven't checked at all what it will be like on other devices, so that's my next task.
      I went to the index page and I get hit with this error:
      Fatal error: Exception: SQLSTATE[HY000] [2002] Connection refused (in /var/sites/b/mysite.org/public_html/wire/core/ProcessWire.php line 377) #0 /var/sites/b/mysite.org/public_html/wire/core/ProcessWire.php(209): ProcessWire\ProcessWire->load(Object(ProcessWire\Config)) #1 /var/sites/b/mysite.org/public_html/index.php(52): ProcessWire\ProcessWire->__construct(Object(ProcessWire\Config)) #2 {main} in /var/sites/b/mysite.org/public_html/index.phpon line 64
      All I have done from the last code update was change a links target to blank, pushed the update and tested it. Now my site is broken? Went back and changed to the original file and I'm still getting the error.
      I really don't understand what has happened. I am an ok {6 months} web developer with this being my first experience of PW.
      All help is appreciated 🙂
    • By EyeDentify
      Hello Fellow PW Fans and Gurus.

      I have run into a problem where i have created a template without a template file associated with it.
      Now i would like to HAVE a template file associated with it, not an alternate but as the main template file.

      So i thought, that's easy, I upload the template file and change it in template settings but I only get the Alternate template file setting?

      Ok so maybe i can change it via the API , so i wrote some code for this.
      <?PHP function changeTemplate($pages = null, $templates = null) { /* get the pages to change template on */ $logItems = $pages->findMany('template=tmp_log_item'); /* get the template object for our desired template */ $Template = $templates->get('tmp_new_log_item'); foreach($logItems AS $key => $logData) { /* use template object $Template to set template */ $logData->template = $Template; /* save page item */ $logData->save(); } } /* do the magic change */ changeTemplate(); ?> Needless to say, PW was not happy about this, It throw up an error message:
      Error: Exception: Invalid value sent to Page::setTemplate (in /home/virtual/mydomain/public_html/wire/core/Page.php line 1782)
      Now from what i understand i gave $logData->template the wrong type of value.
      So what should the value be to correct this?
      When I read the docs at: https://processwire.com/api/ref/page/
      the value can be "string" or "Template".

      So i gave it a string and it went haywire.

      How should I approach this cause I realy dont want to manually change template file on över 50 plus pages

      Maybe I should use the Template class to create the value that the API wanted, but I am unsure how to use it.

      Thank you for any info you can give me on this.

      If you want more info to help me, ask and I will try my best to give it to you.
      Updated and Solved
      I solved the problem and have updated the code to reflect this.

      Essentialy i used the get() method of $templates to get the template object for my template i wanted to use using the template name.
      I gave the Object to the $logData->template property and all went well.
      We learn something new every day.
      One Note:
      The template file you want to use must have been uploaded and added as a template in the Template section in the Admin for this code to work as expected.
    • By chcs
      After running this code in my module's __install() the template_id is not set. Any ideas why?  (The export contents from the template created and the one that I built with Admin are the same. Both exports have the template_id is set to mm_state. When I go to edit the field in Admin, the template is not showing on the Input tab and it doesn't show the template title on a page using the template that uses this field. Using processwire 3.0.98)

      // mm_state
      $field = $this->fields->get('mm_state');
      if (!@$field->id) $field = new Field();
      $field->type = $this->modules->get("FieldtypePage");
      $field->name = 'mm_state';
      $field->label = 'State';
      $field->derefAsPage = 2;
      $field->inputfield = "InputfieldSelect";
      $this->fields->save($field);
      $field->labelFieldName = 'title';
      $field->template_id = 'mm_state';
      $field->columnWidth = 100;
      $field->required = true;
      $this->fields->save($field);
    • By chcs
      I have a page with a good amount of fields on it, I have many other pages in the same template that have no issue, but one particular page just doesn't save changes to any of the fields. 
      I installed the clone page module, cloned the page, and everything cloned fine... but still when i try to make changes to the clone nothing saves either (just like the original page). 
      I expect some kind of notification to show up saying it saved, or not, but i get nothing as if the page reloaded fresh. The failure is silent, and there's nothing in the logs to direct me.
      This is a real head scratcher and I'm not sure how I should go about troubleshooting it further. Can someone help?
      Thank you kindly.