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

onBeforeWrite: copy value from a field to another field...


Reply

5 Posts   1800 Views

Avatar
biapar

29 June 2010 at 8:53pm (Last edited: 3 July 2010 1:26am), Forum Moderator, 435 Posts

Hi,

is it possible, before save a page, to copy data from one field to another field?
That is I've a field A and a field B. After compiled A and leave field A, value of A must be filled in field B.

Thank you

Avatar
biapar

3 July 2010 at 1:26am (Last edited: 3 July 2010 1:26am), Forum Moderator, 435 Posts

Into product.php controller, I write:

protected function onBeforeWrite() {

parent::onBeforeWrite();

if ($this->ID) {

$this->record['QuotaDisponibile'] = $this->record['PriceProduct'];
// fill in MetaDescription without any tags

if ($this->record['Content']) {

$this->record['MetaDescription'] = strip_tags($this->record['Content']);
}

if ($this->record['Content']) {

$this->record['MetaKeywords'] = self::calculateKeywords($this->record['Content'], 4, 15);
}
}

$this->record['QuotaDisponibile'] = $this->record['PriceProduct'];
}

but don't work?
How can view debug? Is there a debug type Visual Studio? -:)

Avatar
MarcusDalgren

3 July 2010 at 4:25am Community Member, 288 Posts

If I remember correctly $this->record refers to the database record and since this is onBeforeWrite it hasn't been written to the database yet. Instead of writing $this->record['QuotaDisponibile'] do $this->QuotaDisponibile instead. The same goes for all your other $this->record calls.

Avatar
biapar

3 July 2010 at 7:42pm (Last edited: 8 July 2010 1:07am), Forum Moderator, 435 Posts

The situation don't change.....

Avatar
Simkim

14 July 2010 at 8:02am Community Member, 9 Posts

Hi,

I have made something similar. In your function onBeforeWrite, you have to retrieve the value of the field, and after that you can copy it to the new field. Here you have the functions I have used:

function onBeforeWrite(){
$firstField = $this->getField('yourFieldName');
$this->setField('yourNewFieldName', $firstField);

/*You can also use the setCastedField function*/

parent::onBeforeWrite();
}

Hope this will help you!

Regards,

Mercedes