src/ORM/DatabaseAdmin.php
public function cleanup()
    {
        $baseClasses = [];
        foreach (ClassInfo::subclassesFor(DataObject::class) as $class) {
            if (get_parent_class($class) == DataObject::class) {
                $baseClasses[] = $class;
            }
        }

        $schema = DataObject::getSchema();
        foreach ($baseClasses as $baseClass) {
            // Get data classes
            $baseTable = $schema->baseDataTable($baseClass);
            $subclasses = ClassInfo::subclassesFor($baseClass);
            unset($subclasses[0]);
            foreach ($subclasses as $k => $subclass) {
                if (!DataObject::getSchema()->classHasTable($subclass)) {
                    unset($subclasses[$k]);
                }
            }

            if ($subclasses) {
                $records = DB::query("SELECT * FROM \"$baseTable\"");


                foreach ($subclasses as $subclass) {
                    $subclassTable = $schema->tableName($subclass);
                    $recordExists[$subclass] =
                        DB::query("SELECT \"ID\" FROM \"$subclassTable\"")->keyedColumn();
                }

                foreach ($records as $record) {
                    foreach ($subclasses as $subclass) {
                        $subclassTable = $schema->tableName($subclass);
                        $id = $record['ID'];
                        if (($record['ClassName'] != $subclass)
                            && (!is_subclass_of($record['ClassName'], $subclass))
                            && isset($recordExists[$subclass][$id])
                        ) {
                            $sql = "DELETE FROM \"$subclassTable\" WHERE \"ID\" = ?";
                            echo "<li>$sql [{$id}]</li>";
                            DB::prepared_query($sql, [$id]);
                        }
                    }
                }
            }
        }
    }
public function cleanup()
    {
        $baseClasses = [];
        foreach (ClassInfo::subclassesFor(DataObject::class) as $class) {
            if (get_parent_class($class) == DataObject::class) {
                $baseClasses[] = $class;
            }
        }

        $schema = DataObject::getSchema();
        foreach ($baseClasses as $baseClass) {
            // Get data classes
            $baseTable = $schema->baseDataTable($baseClass);
            $subclasses = ClassInfo::subclassesFor($baseClass);
            unset($subclasses[0]);
            foreach ($subclasses as $k => $subclass) {
                if (!DataObject::getSchema()->classHasTable($subclass)) {
                    unset($subclasses[$k]);
                }
            }

            if ($subclasses) {
                $records = DB::query("SELECT * FROM \"$baseTable\"");


                foreach ($subclasses as $subclass) {
                    $subclassTable = $schema->tableName($subclass);
                    $recordExists[$subclass] =
                        DB::query("SELECT \"ID\" FROM \"$subclassTable\"")->keyedColumn();
                }

                foreach ($records as $record) {
                    foreach ($subclasses as $subclass) {
                        $subclassTable = $schema->tableName($subclass);
                        $id = $record['ID'];
                        if (($record['ClassName'] != $subclass)
                            && (!is_subclass_of($record['ClassName'], $subclass))
                            && isset($recordExists[$subclass][$id])
                        ) {
                            $sql = "DELETE FROM \"$subclassTable\" WHERE \"ID\" = ?";
                            echo "<li>$sql [{$id}]</li>";
                            DB::prepared_query($sql, [$id]);
                        }
                    }
                }
            }
        }
    }
public function cleanup ( ) { $ baseClasses = [ ] ; foreach ( ClassInfo :: subclassesFor ( DataObject :: class ) as $ class ) { if ( get_parent_class ( $ class ) == DataObject :: class ) { $ baseClasses [ ] = $ class ; } } $ schema = DataObject :: getSchema ( ) ; foreach ( $ baseClasses as $ baseClass ) { // Get data classes $ baseTable = $ schema -> baseDataTable ( $ baseClass ) ; $ subclasses = ClassInfo :: subclassesFor ( $ baseClass ) ; unset ( $ subclasses [ 0 ] ) ; foreach ( $ subclasses as $ k => $ subclass ) { if ( ! DataObject :: getSchema ( ) -> classHasTable ( $ subclass ) ) { unset ( $ subclasses [ $ k ] ) ; } } if ( $ subclasses ) { $ records = DB :: query ( "SELECT * FROM \"$baseTable\"" ) ; foreach ( $ subclasses as $ subclass ) { $ subclassTable = $ schema -> tableName ( $ subclass ) ; $ recordExists [ $ subclass ] = DB :: query ( "SELECT \"ID\" FROM \"$subclassTable\"" ) -> keyedColumn ( ) ; } foreach ( $ records as $ record ) { foreach ( $ subclasses as $ subclass ) { $ subclassTable = $ schema -> tableName ( $ subclass ) ; $ id = $ record [ 'ID' ] ; if ( ( $ record [ 'ClassName' ] != $ subclass ) && ( ! is_subclass_of ( $ record [ 'ClassName' ] , $ subclass ) ) && isset ( $ recordExists [ $ subclass ] [ $ id ] ) ) { $ sql = "DELETE FROM \"$subclassTable\" WHERE \"ID\" = ?" ; echo "<li>$sql [{$id}]</li>" ; DB :: prepared_query ( $ sql , [ $ id ] ) ; } } } } } }
Remove invalid records from tables - that is, records that don't have
corresponding records in their parent class tables.
Remove invalid records from tables - that is records that don t have corresponding records in their parent class tables .