Jump to content

Create new page, if it doesn´t exist


alejandro
 Share

Recommended Posts

Hello,

using the code

$fwd = $pages->get("title=$transaction_id");

if($fwd) {
	$session->redirect($fwd->url);
} else {
	$p = new Page();
	$p->parent = $pages->get("1350");
	$p->template = $templates->get("receipt");
	$p->title = $transaction_id;
	$p->email = $email;
	$p->fcproduct = $product;
	$p->fcprice = $price;
	$p->save();
	echo $p->render();
}

I'm trying to generate a new page checkin if exists previosly. If exists is redirected correctly, if it doesn´t the page it's not created. But using just:

	$p = new Page();
	$p->parent = $pages->get("1350");
	$p->template = $templates->get("receipt");
	$p->title = $transaction_id;
	$p->email = $email;
	$p->fcproduct = $product;
	$p->fcprice = $price;
	$p->save();
	echo $p->render();

The page is created and rendered. I can´t get why the two pieces doesn´t like to be friends...

Thanks in advance for any help, Alejandro

Link to comment
Share on other sites

Hi Alejandro!

That else-block is never executed, that's why the page does not get created and rendered. This is because $pages->get() "Returns a Page, or a NullPage if not found.". So $fwd is always an object (either a Page or a NullPage) and "if($fwd)" is always true.

Test for the page id to see if you got a real page, like this:

if($fwd->id) {
  ...
} else {
  ...
}

(And remember to sanitize $transaction_id if it's part of user input.)

  • Like 2
Link to comment
Share on other sites

 
So if I understand correctly, if " $fwd->id " is empty, NullPage is not returned.

Almost :)

If there is a page found, a Page object is returned, which always has an id > 0.

If there is no page found, a NullPage object is returned, which always has the id = 0.

the id is either equal to zero or greater, but never empty.

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