Add Page programatically

Permalink
Hi All,

In desparate need of help here.

I am trying to create a page at root level after submitting a form.

I am doing the following (of which I have tried multiple ways)

//$arrPage is set above with name hande and date public
$parentPage = Page::getByPath("/index");
$pt = CollectionType::getByHandle('pagetype');      
$newPage = $parentPage->add($pt,$arrPage);


however on execution I am getting this error

mysql error: [1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1] in EXECUTE("select max(cDisplayOrder) from Pages where cParentID = LIMIT 1")

HELP!

davidofadelaide
 
jero replied on at Permalink Reply
jero
You code looks OK to me, but I managed to reproduce the error by using an invalid value in the getByPath() method - perhaps "/index" isn't what you want?

If this is supposed to be the home page, then that page doesn't have a path. You'd be better off using:

$parent=Page::getByID(1);


This works for me as a tools url:

<?php
Loader::model('page');
Loader::model('collection_types');
$ct=CollectionType::getByHandle('left_sidebar');
#$parent = Page::getByPath('/about');
$parent = Page::getByID(1);
$bt = BlockType::getByHandle('content');
$data=array();
$data['pkgID']=0;
$data['cName'] = 'Test Page';
$page = $parent->add($ct,$data);
var_dump($page);