Jump to content

Issue whit api: creating page inside page class


saleo
 Share

Recommended Posts

Hello. I don't know if this is a bug or if I'm using a bad practice. I'm testing out latest pw 3.0.167.

I'm used to write this whenever I need to create a new Page from api

$p = new Page();
$p->template = 'upload';
$p->title = 'my new page';
$p->parent = 1019;
$p->save();

The following code is quite similar, yet when used inside a custom page class is generating a nasty issue.

$upload = new Page();
$upload->template = 'upload';
$upload->parent = wire()->pages->get("template=uploads");

$upload->title = time() . '-' . $email;

$upload->nome = $nome;
$upload->cognome = $cognome;
$upload->email = $email;


$upload->addStatus(Page::statusUnpublished);
$upload->save();

After execution, the admin stops working with this exception

Umm… Fatal Error: Uncaught Error: Call to a member function getPageClass() on string in wire/core/PagesLoader.php:945

#0 wire/core/PagesLoader.php (421): PagesLoader->getById(Array, NULL)
#1 wire/core/Pages.php (255): PagesLoader->find('parent_id=1019,...', Array)
#2 wire/core/Wire.php (400): Pages->___find('parent_id=1019,...', Array)
#3 wire/core/WireHooks.php (823): Wire->_callMethod('___find', Array)
#4 wire/core/Wire.php (465): WireHooks->runHooks(Object(Pages), 'find', Array)
#5 wire/core/Page.php (4577): Wire->__call('find', Array)
#6 wire/core/PageTraversal.php (130): Page- (line 945 of wire/core/PagesLoader.php)

This error message was shown because: you are logged in as a Superuser. Error has been logged.

Looking in the database, I notice that the new created page has no template assigned

1372333231_Schermata2020-10-02alle06_31_30.thumb.png.503b6dd6ed13a74dfc8f62dc98cf457e.png

If I remove that row from the db, the admin resumes working nicely.

Now if I change this

$upload->template = 'upload';

to this

$upload->template = wire()->templates->get('upload');

everything goes as expected. Any ideas?

Thank you

Leo

 

Link to comment
Share on other sites

Looks to me that we always have to provide a template object and not a templates name when not in template file scope. The reason why this is working in template file scope may have to do with some extra work and attention of pw behind the scenes in template file scope. (like outputformatting etc.) But this is only my intuitive idea, no valid technical description. ?

 

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