Source of file DataIntegrityTestDefaultEntries.php
Size: 2,010 Bytes - Last Modified: 2021-12-23T10:39:18+00:00
/var/www/docs.ssmods.com/process/src/src/Api/DataIntegrityTestDefaultEntries.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 | <?php namespace Sunnysideup\DataIntegrityTest\Api; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Core\Convert; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DB; use SilverStripe\View\ViewableData; /** * ### @@@@ START REPLACEMENT @@@@ ### * WHY: upgrade to SS4 * OLD: extends Object (ignore case) * NEW: extends ViewableData (COMPLEX) * EXP: This used to extend Object, but object does not exist anymore. You can also manually add use Extensible, use Injectable, and use Configurable * ### @@@@ STOP REPLACEMENT @@@@ ### */ class DataIntegrityTestDefaultEntries { public static function update($baseTable, $field, $value, $id = 0, $replace = false, $addLive = false) { $object = DataObject::get_one($baseTable); if ($object) { $tableArray = [$baseTable]; if ($object instanceof SiteTree) { $tableArray[] = $baseTable . '_Live'; } foreach ($tableArray as $table) { $value = Convert::raw2sql($value); $sql = "UPDATE \"${table}\" SET \"${table}\".\"${field}\" = '${value}'"; $where = []; if ($id) { $where[] = " \"${table}\".\"ID\" = " . $id; } if (! $replace) { $where[] = " \"${table}\".\"${field}\" IS NULL OR \"${table}\".\"${field}\" = '' OR \"${table}\".\"${field}\" = 0 "; } $wherePhrase = ''; if (count($where)) { $wherePhrase = ' WHERE ( ' . implode(') AND (', $where) . ' )'; } $result = DB::query("SELECT COUNT(\"${table}\".\"ID\") C FROM \"${table}\" " . $wherePhrase); if ($result && $result->value()) { $sql .= $wherePhrase; DB::query($sql); DB::alteration_message("Updated ${field} in ${table} to ${value} ", 'added'); } } } } } |