Jump to content

created property


Frank Vèssia
 Share

Recommended Posts

Some properties are only populated at page load time. When you create a new page in memory and save it, that page has still never been loaded from the database. As a result, it won't have created/modified properties since those are DB generated. PW doesn't automatically reload a page from the DB when you save it. But you can reload it yourself:

$page = $pages->get($page->id); 

However, I think an even better strategy is to perform a redirect after you've committed any kind of major changes. That ensures that you are dealing with a fresh copy of everything and prevents a browser reload/refresh from re-posting your form and potentially creating a duplicate. So I would do something like this:

<?php

$template = $templates->get('new_page_template'); 
$parent = $pages->get('/path/to/parent/'); 

if($input->post->submit == 'save') {

    $p = new Page();
    $p->parent = $parent;
    $p->template = $template; 

    // ...populate the page...
    
    if($p->save()) {
        $session->newPageID = $p->id; 
        $session->redirect('./'); 
    } else {
        echo "error saving page";
    }

} else if($session->newPageID) {

    $p = $pages->get($session->newPageID);

    // a little security check, that may not be necessary since we're using $session
    // but i always veer on the side of more security.
    if($p->parent->id == $parent->id && $p->template === $template) { 
        // display the results
    }
}
  • Like 1
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...