Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » onBeforeWrite: copy value from a field to another field...

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

Page: 1
Go to End
Author Topic: 1589 Views
  • biapar
    Avatar
    Forum Moderator
    435 Posts

    onBeforeWrite: copy value from a field to another field... Link to this post

    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

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: onBeforeWrite: copy value from a field to another field... Link to this post

    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? -

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: onBeforeWrite: copy value from a field to another field... Link to this post

    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.

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: onBeforeWrite: copy value from a field to another field... Link to this post

    The situation don't change.....

  • Simkim
    Avatar
    Community Member
    9 Posts

    Re: onBeforeWrite: copy value from a field to another field... Link to this post

    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

    1589 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.