Version: current

     1 <?php
     2 /*
     3  * Задача заполнения значний новых полей LogItem
     4  *
     5  * @author menedem
     6  */
     7 class FillOldLogDataTask extends MigrationTask {
     8     protected $title = "Fill old LogItem data";
     9
    10     protected $description = "Fill new fields of LogItem for old entries";
    11
    12     /**
    13      * Run the conversion.
    14      *
    15      * @param HTTPRequest
    16      */
    17     function run($request) {
    18         $items = DataObject::get('LogItem', "ItemTitle = '' OR MemberLogin = ''");
    19         if ($items) {
    20             foreach($items as $item) {
    21                 $changes = false;
    22                 $update = array();
    23                 if (!$item->getField('ItemTitle')) {
    24                     if ($item->Class && class_exists($item->Class) && $data = DataObject::get_by_id($item->Class, $item->ItemID)) {
    25                         $update[] = "ItemTitle = '" . Convert::raw2sql(trim($data->Title)) . "'";
    26                         $changes = true;
    27
    28                     }
    29                 }
    30                 if (!$item->MemberLogin) {
    31                     if ($member = DataObject::get_by_id('Member', $item->MemberID)) {
    32                         $update[] = "MemberLogin = '" . Convert::raw2sql(trim($member->Title)) . "'";
    33                         $changes = true;
    34                     }
    35                 }
    36                 if ($changes) {
    37                     DB::Query('UPDATE LogItem SET ' . implode(', ', $update) . ' WHERE ID = ' . $item->ID);
    38                 }
    39
    40             }
    41         }
    42         unset($items);
    43         echo "Old LogItems filled finished!\n";
    44     }
    45
    46 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -