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.

Customising the CMS /

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

onAfterWrite() Apache lost


Go to End


5 Posts   1801 Views

Avatar
snaip

Community Member, 181 Posts

8 March 2011 at 8:31am

Edited: 08/03/2011 8:33am

i try to use onAfterWrite()

<?php

class ProduktKolory extends DataObject {

    static $db = array(
        'Nazwa' => 'Varchar',
        'URL' => 'Varchar'
    );
    static $has_one = array(
        'Produkt' => 'Produkt',
        'Kolor' => 'ModulKolory'
    );

    public function getCMSFields_forPopup() {

        $source_kolory = DataObject::get('ModulKolory');
        $kolory = $source_kolory->toDropDownMap('IkonaID','Nazwa');

        return new FieldSet(
                new DropdownField('KolorID','Wybierz kolor:',$kolory)
        );
    }

    public function onAfterWrite() {

        $filter = "IkonaID = $this->KolorID";
        $kolor = DataObject::get_one('ModulKolory',$filter);

        $filter = "ID = $this->KolorID";
        $file = DataObject::get_one('File',$filter);

        $filter = "ID = $this->ID";
        $produkt_kolor = DataObject::get_one('ProduktKolory',$filter);
        $produkt_kolor->Nazwa = $kolor->Nazwa;

        $produkt_kolor->URL = $file->Filename;
        $produkt_kolor->write();

        return parent::onAfterWrite();

    }

}

?>


but when i save new rocord the Apache server get lost

Avatar
Willr

Forum Moderator, 5523 Posts

9 March 2011 at 10:15pm

Creating a ProduktKolory inside the onAfterWrite() will trigger another onAfterWrite(), it's probably stuck in a horrible loop thanks to $produkt_kolor = DataObject::get_one('ProduktKolory',$filter); always firing.

Avatar
WebInt

Community Member, 14 Posts

23 June 2011 at 4:05am

Can anyone think of a workaround for that?

Perhaps a writeWithoutEvent() function would be the remedy?

Anyway, really need to figure out how to get around this pronto!

function onAfterWrite() {
		$fields = $this->getField($this->dbObject("Region1"));
		$this->setField("Regions", $fields );
		$this->write();
		return parent::onAfterWrite();
}	

Avatar
zenmonkey

Community Member, 545 Posts

15 July 2011 at 6:58am

I've done it a by using a plain jane sql query to do the write, that way its not using the write method

Avatar
Willr

Forum Moderator, 5523 Posts

15 July 2011 at 9:04am

Well if you set the field, never call write the field *should* still get saved as the write is executed after that function is run.