Jump to content

[SOLVED] Call to a member function pageName() on null


ziudeso
 Share

Recommended Posts

Hi all, before submitting my question I wanna compliment with the Processwire dev team!
You guys not only have managed to create a crazy-simple framework in its core concepts but are empowering people like me to understand clean and pure PHP scripting!

Here's my question:
I'm having troubles to implement a simple initDb.sh file that sets up the entire page hierarchy and structure.

The code is:

#!/usr/bin/php
<?php
include("./index.php"); // bootstrap ProcessWire
...
function createMockUsers() {
  $item = new ProcessWire\User();
  $item->setOutputFormatting(false);
  $item->name = $wire->sanitizer->pageName("newname");
  $item->pass = "yo12345";
  $item->email = $wire->sanitizer->email("example@processwire.com");
  $item->addRole('guest');
  $item->save();
}
...
createMockUsers();

The error is:

Error: Call to a member function pageName() on null

Additionally, is there a better way to initialize a Processwire structure rather than using an external script? Aand, is there a nicer syntax for instantiating new classes (not like "  $item = new ProcessWire\User();")


Thank you guys for your support and the wonderful piece of software you made!

Link to comment
Share on other sites

If you define the namespace on top of your script, you wont have to prefix functions with \Processwire\

#!/usr/bin/php
<?php namespace ProcessWire; // remember to include namespace

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

function createMockUsers() {
  $u = new User();
  $u->of(false); // short for outputFormatting
  
  // inside closure you cannot access global $wire variable
  // unless you set function myFunction() use ($wire) {} 
  // but you can call wire() function instead

  $u->name = wire()->sanitizer->pageName("newname");
  $u->pass = "yo12345";
  $u->email = wire()->sanitizer->email("example@processwire.com");
  $u->addRole('guest');
  $u->save();
}

// ...
createMockUsers();

 

  • Like 3
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...