Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Default values for relation's pre-existing records

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

Page: 1
Go to End
Author Topic: 275 Views
  • svandragt
    Avatar
    Community Member
    43 Posts

    Default values for relation's pre-existing records Link to this post

    OK I have added a relation to an existing DataObject with records. The relation is displayed in the CMS through a DropdownField.
    I have set the default value for this relation for new records using populateDefaults(), say to 3.

    The problem I am having how would I change the default value for records that existed before I added this relationship (as they show up as blank in the GridField and with an ID of 0 in the database for the relationID column).

    I can edit the database directly and update all records with ID 0 but an solution coded in the dataobject would be preferable, if this is possible? I also don't want to ask my editors to re-save all records one by one.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Default values for relation's pre-existing records Link to this post

    Running a SQL query directly would be less code (and faster) but if you want to do it via the framework one way to do migrations is to use a BuildTask.

    <?php

    class DoStuff extends BuildTask() {

    public function run($request) {
    $records = Page::get('Page')->filter(array(
    "SomeID" => 0
    ));

    foreach($records as $record) {
    $record->SomeID = 3;
    $record->write();
    }

    }

    Then run dev/tasks/DoStuff to perform the operation.

  • svandragt
    Avatar
    Community Member
    43 Posts

    Re: Default values for relation's pre-existing records Link to this post

    Ah great hadn't used build tasks before, this example is just what I need to get to grips with that also. Thanks so much Willr!

    275 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.