Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Inserting pages directly to the SiteTree


Go to End


4 Posts   2155 Views

Avatar
Qbert

Community Member, 5 Posts

3 September 2008 at 4:12pm

Hi,

I have read a few topics on this in the forum and cant find a clear direction for what i need to do. Basically I am extending the ecommerce functionality as my client has custom products (that the user generates themselves via a form) < this bit is all working and ready, what i need to do is once the custom product has been generated - insert this product into the SiteTree and Product tables (both Normal and Live) without any manual intervention, this should be seemless for the user, once i have inserted the product (pages) then it will be added to the users basket, and the admin will have a reference for it in the backend (custom products will be hidden to users).

So far i have all this functionality down, appart from automatically inserting into the database.

the way i see it is i could insert into SiteTree, SiteTree_Live, Product and Product_Live, but I've found posts that are contrary to this method, suggesting that we should use Dataobject or even just insert into the non live versions and then publishall (the latter of which isnt appropriate in this case unless i can execute publish all without being logged in).

Could you please advise as to the best course of action to acheive the above?

Cheers and thanks in advance.

Avatar
Qbert

Community Member, 5 Posts

3 September 2008 at 8:51pm

Hi,

I'm still looking into this and i found this code construct in SiteTree.php in saphire:

$homepage = new Page();

$homepage->Title = _t('SiteTree.DEFAULTHOMETITLE', 'Home');
$homepage->Content = _t('SiteTree.DEFAULTHOMECONTENT', '<p>Welcome to SilverStripe! This is the default homepage. You can edit this page by opening <a href="admin/">the CMS</a>. You can now access the <a href="http://doc.silverstripe.com">developer documentation</a>, or begin <a href="http://doc.silverstripe.com/doku.php?id=tutorials">the tutorials.</a></p>');
$homepage->URLSegment = "home";
$homepage->Status = "Published";
$homepage->write();
$homepage->publish("Stage", "Live");
$homepage->flushCache();

It looks as though this is how the demo pages are setup automatically, can the same simply be done for both a new Page(); type and a new Product(); type directly from a page controller file? I want to ask first incase i'm about to do something really stupid - but i guess can always backup and try anyway :)

Avatar
Qbert

Community Member, 5 Posts

12 September 2008 at 7:34pm

Edited: 12/09/2008 7:36pm

Hi,

I'm nearly there with this but have a problem autosubmitting to the basket. can you please help on this as i'm currently going round in circles.. code follows:

$UID=DB::query("SELECT MAX(ID) FROM Product_Live")->value();
$UID++;
//echo"<h2>UID:: $UID</h2>";
$title="Custom Wig ".$UID;
$urlsegment="custom-wig-$UID";

//INSERT THE CUSTOM WIG TO THE SITETREE ETC (yes wigs! crazy thing but there you go...)

$productPage = new Product();
$productPage->Title = $title;
$productPage->Content = $wig_info;
$productPage->URLSegment = $urlsegment;
//$productPage->ParentID = $productgroupPageLvl2->ID;
$productPage->ParentID = 42;
$productPage->Weight = '1';
//$productPage->Model = 'Joe Bloggs';
$productPage->Price = $subtotal;
$productPage->AllowPurchase = 1;
$productPage->ImageID=25;
//$productPage->FeaturedProduct = 0;
$productPage->Status = 'Published';
$productPage->write();
$productPage->publish('Stage', 'Live');

//fix broken inseterts into non live tables (for some reason the above doesnt complete inserts to SiteTree or Product Tables properly:
DB::query("UPDATE `SiteTree` SET `ClassName`= 'Product', `LastEdited` = NOW(), `URLSegment` = '$urlsegment', `Title` = '$title',`MenuTitle` = '$title', `Content`='$wig_info', `ShowInMenus`='1', `ShowInSearch`='1', `ProvideComments`='0', `Sort`='2', `Status`='Published', `ParentID`='42', `Version`='1' WHERE `ID` ='$UID' LIMIT 1 ;");
//update the Product record:
DB::query("INSERT INTO `Product` (`ID`, `Price`,`Weight`,`AllowPurchase`,`ImageID`) VALUES ('$UID','$subtotal','1','1','25');");

//THIS IS THE OBJECT OF CONTENTION:: results in a server failed to respond to request.
$myproduct=DataObject::get("Product", "`ID`='$UID'");
ShoppingCart::add_new_item_quantity(new Product_OrderItem($myproduct, $quantity));

Can you not get a dataobject within a page function itself? if not, how can i retrieve a correct product object in order to insert it to the basket?

One thing i cant do here is simply redirect to the page to insert, because i also have to insert express delivery options if they are selected.

Please, i would really like some help on this as i said, i am absolutely going round in circles here....

Cheers, Q

Avatar
Willr

Forum Moderator, 5523 Posts

12 September 2008 at 9:09pm

//THIS IS THE OBJECT OF CONTENTION:: results in a server failed to respond to request.
$myproduct=DataObject::get("Product", "`ID`='$UID'");

Ok if I read that correctly maybe just try this instead - note the Prefixed Product table - this will prevent some MySQL ambiguous column errors

$myproduct=DataObject::get("Product", "`Product`.ID='$UID'");