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.

Customising the CMS

onAfterWrite() Apache lost


Reply

5 Posts   960 Views

Avatar
snaip

8 March 2011 at 8:31am (Last edited: 8 March 2011 8:33am), Community Member, 180 Posts

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

9 March 2011 at 10:15pm Forum Moderator, 5511 Posts

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

23 June 2011 at 4:05am Community Member, 11 Posts

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

15 July 2011 at 6:58am Community Member, 528 Posts

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

15 July 2011 at 9:04am Forum Moderator, 5511 Posts

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.