Jump to content

Strange error


cpx3
 Share

Recommended Posts

Hi everyone,

I have quite a strange problem. When I use the following code with a terminal php command, it produces the following error message:

Fatal Error: Uncaught Error: Call to a member function of() on int in /site/templates/process_pagefields.php:20

#0 {main}

  thrown (line 20 of /site/templates/process_pagefields.php)

 When I use it with Tracy it works fine from time to time, but sometimes it echoes "0", too. Any ideas what I am doing wrong?

Thanks in advance,

Bernhard

 

Here goes the code

include("/html/index.php"); // bootstrap ProcessWire
// Get derefAsPage property of page_field_b to distinguish multiple/single page fields
$deref = $fields->get('select_related_articles')->derefAsPage;
foreach($pgs as $p) {
 // For this page, get the pages selected in page_field_a
 $selected_pages = $p->select_author;
 // Standardise $selected_pages to PageArray in case of "single" field
 if($selected_pages instanceof Page) $selected_pages = $selected_pages->and();
 foreach($selected_pages as $selected_page) {
 // Add this page to page_field_b on the selected page
 $selected_page->of(false);
 if($deref) {
 $selected_page->select_related_articles = $p;
 echo "Set $p \n";
 } else {
 $selected_page->select_related_articles->add($p);
 echo "Added $p \n";
 }
 $selected_page->save();
 }
 $numPgs--;
 echo $numPgs."\r";
 }

 

Link to comment
Share on other sites

I don’t know what’s going wrong, but maybe I can contribute a couple of things.

Firstly, a more readable version of your code:

include("/html/index.php"); // bootstrap ProcessWire

// Get derefAsPage property of page_field_b to distinguish multiple/single page fields
$deref = $fields->get('select_related_articles')->derefAsPage;

// […] some stuff appears to have been omitted here ($pgs)

foreach ($pgs as $p) {
    // For this page, get the pages selected in page_field_a
    $selected_pages = $p->select_author;
    
    // Standardise $selected_pages to PageArray in case of "single" field
    if ($selected_pages instanceof Page)
        $selected_pages = $selected_pages->and();
        
    foreach ($selected_pages as $selected_page) {
        // Add this page to page_field_b on the selected page
        $selected_page->of(false); // <-- This is the offending line
        
        if ($deref) {
            $selected_page->select_related_articles = $p;
            echo "Set $p \n";
        } else {
            $selected_page->select_related_articles->add($p);
            echo "Added $p \n";
        }
        
        $selected_page->save();
    }
    
    $numPgs--;
    echo $numPgs . "\r";
}

Secondly, I don’t think you even need to turn off output formatting explicitly, because it’s off by default in this bootstrapped context.

Lastly and probably most relevant, the error message tells you that $selected_page is an integer when you try to call of(false) on it. I don’t really see how that would happen given your code, but maybe just dump its value here and there to figure it out? Since it’s the value of a page reference field, it should be Page (perhaps a custom page type), NullPage or PageArray. Btw, if it is a Page or NullPage object and you call and() on it, you’ll get a WireArray, not a PageArray.

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