silverstripe, how to use the doPublish()

I am working with SilverStripe, and I am working on making a newspage. I use the DataObjectAsPage Module( ), I got it working when I use the admin to publish newsitems. Now I want to use the DataObjectManager Module instead of the admin module to manage my news items. But this is where the problem exists. Everything works fine in draft mode, I can make a new newsitem and it shows up in draft. But when I want to publish a newsitem, it won't show up in the live or published mode.

I'm using the following tables: -Dataobjectaspage table, -Dataobjectaspage_live table, -NewsArticle table, -NewsArticle_Live table

The Articles have been inserted while publishing in the Dataobjectaspage table and in the NewsArticle table... But not in the _Live tables...

Seems the doPublish() function hasn't been used while 'Publishing'. So I'm trying the use the following:

function onAfterWrite() {

But when I use this, it gets an error: here is this picture

It seems to be in a loop.... I've got the NewsArticle.php file where I use this function:

 function onAfterWrite() {

This function calls the DataObjectAsPage.php file and uses this code:

function doPublish() {
        if (!$this->canPublish()) return false;

        $original = Versioned::get_one_by_stage("DataObjectAsPage", "Live", "\"DataObjectAsPage\".\"ID\" = $this->ID");
        if(!$original) $original = new DataObjectAsPage();

        // Handle activities undertaken by decorators
        $this->invokeWithExtensions('onBeforePublish', $original);
        $this->Status = "Published";
        //$this->PublishedByID = Member::currentUser()->ID;
        $this->publish("Stage", "Live");

        // Handle activities undertaken by decorators
        $this->invokeWithExtensions('onAfterPublish', $original);

        return true;

And then it goes to DataObject.php file and uses the write function ():

public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) {
    $firstWrite = false;
    $this->brokenOnWrite = true;
    $isNewRecord = false;

    if(self::get_validation_enabled()) {
        $valid = $this->validate();
        if(!$valid->valid()) {
            // Used by DODs to clean up after themselves, eg, Versioned
            throw new ValidationException($valid, "Validation error writing a $this->class object: " . $valid->message() . ".  Object not written.", E_USER_WARNING);
            return false;

    if($this->brokenOnWrite) {
        user_error("$this->class has a broken onBeforeWrite() function.  Make sure that you call parent::onBeforeWrite().", E_USER_ERROR);

    // New record = everything has changed

    if(($this->ID && is_numeric($this->ID)) && !$forceInsert) {
        $dbCommand = 'update';

        // Update the changed array with references to changed obj-fields
        foreach($this->record as $k => $v) {
            if(is_object($v) && method_exists($v, 'isChanged') && $v->isChanged()) {
                $this->changed[$k] = true;

    } else{
        $dbCommand = 'insert';

        $this->changed = array();
        foreach($this->record as $k => $v) {
            $this->changed[$k] = 2;

        $firstWrite = true;

    // No changes made
    if($this->changed) {
        foreach($this->getClassAncestry() as $ancestor) {
            $ancestry[] = $ancestor;

        // Look for some changes to make
        if(!$forceInsert) unset($this->changed['ID']);

        $hasChanges = false;
        foreach($this->changed as $fieldName => $changed) {
            if($changed) {
                $hasChanges = true;

        if($hasChanges || $forceWrite || !$this->record['ID']) {

            // New records have their insert into the base data table done first, so that they can pass the
            // generated primary key on to the rest of the manipulation
            $baseTable = $ancestry[0];

            if((!isset($this->record['ID']) || !$this->record['ID']) && isset($ancestry[0])) {  

                DB::query("INSERT INTO \"{$baseTable}\" (\"Created\") VALUES (" . DB::getConn()->now() . ")");
                $this->record['ID'] = DB::getGeneratedID($baseTable);
                $this->changed['ID'] = 2;

                $isNewRecord = true;

            // Divvy up field saving into a number of database manipulations
            $manipulation = array();
            if(isset($ancestry) && is_array($ancestry)) {
                foreach($ancestry as $idx => $class) {
                    $classSingleton = singleton($class);

                    foreach($this->record as $fieldName => $fieldValue) {
                        if(isset($this->changed[$fieldName]) && $this->changed[$fieldName] && $fieldType = $classSingleton->hasOwnTableDatabaseField($fieldName)) {
                            $fieldObj = $this->dbObject($fieldName);
                            if(!isset($manipulation[$class])) $manipulation[$class] = array();

                            // if database column doesn't correlate to a DBField instance...
                            if(!$fieldObj) {
                                $fieldObj = DBField::create('Varchar', $this->record[$fieldName], $fieldName);

                            // Both CompositeDBFields and regular fields need to be repopulated
                            $fieldObj->setValue($this->record[$fieldName], $this->record);

                            if($class != $baseTable || $fieldName!='ID')

                    // Add the class name to the base object
                    if($idx == 0) {
                        $manipulation[$class]['fields']["LastEdited"] = "'".SS_Datetime::now()->Rfc2822()."'";
                        if($dbCommand == 'insert') {
                            $manipulation[$class]['fields']["Created"] = "'".SS_Datetime::now()->Rfc2822()."'";
                            //echo "<li>$this->class - " .get_class($this);
                            $manipulation[$class]['fields']["ClassName"] = "'$this->class'";

                    // In cases where there are no fields, this 'stub' will get picked up on
                    if(self::has_own_table($class)) {
                        $manipulation[$class]['command'] = $dbCommand;
                        $manipulation[$class]['id'] = $this->record['ID'];
                    } else {
            $this->extend('augmentWrite', $manipulation);

            // New records have their insert into the base data table done first, so that they can pass the
            // generated ID on to the rest of the manipulation
            if(isset($isNewRecord) && $isNewRecord && isset($manipulation[$baseTable])) {
                $manipulation[$baseTable]['command'] = 'update';


            if(isset($isNewRecord) && $isNewRecord) {
            } else {


            $this->changed = null;
        } elseif ( $showDebug ) {
            echo "<b>Debug:</b> no changes for DataObject<br />";
            // Used by DODs to clean up after themselves, eg, Versioned

        // Clears the cache for this object so get_one returns the correct object.

        if(!isset($this->record['Created'])) {
            $this->record['Created'] = SS_Datetime::now()->Rfc2822();
        $this->record['LastEdited'] = SS_Datetime::now()->Rfc2822();
    } else {
        // Used by DODs to clean up after themselves, eg, Versioned

    // Write ComponentSets as necessary
    if($writeComponents) {
    return $this->record['ID'];

Look at the $this->onAfterWrite(); It probably goes to my own function on NewsArticle.php and there starts the loop! I'm not sure though, so i could need some help!!

Does anyone knows how to use the doPublish() function?


The reason that is happening is that in the DataObjectAsPage::publish() method, it is calling ->write() - line 11 of your 3rd code sample.

So what happens is it calls ->write(), at the end of ->write() your onAfterWrite() method is called, which calls publish(), which calls write() again.

If you remove the onAfterWrite() function that you've added, it should work as expected.

The doPublish() method on DataObjectAsPage will take care of publishing from Stage to Live for you.

Need Your Help

Raphael arc animation is morphing instead of following arc path

javascript animation svg raphael

I have been able to successfully animate an arc along the path of the arc using this code

Launching my deployed my app gets "has stopped working" error. How do I debug this?

.net debugging deployment crash

I deployed a VB.Net app and ran it and I get 'AppName has stopped working" "Windows is checking for a solution to the problem" along with a Cancel button under Windows 7. Under XP I am only getting...