src/Versioned.php
protected function cleanupVersionedOrphans($baseTable, $childTable)
    {
        // Avoid if disabled
        if ($this->owner->config()->get('versioned_orphans_disabled')) {
            return;
        }

        // Skip if tables are the same (ignore case)
        if (strcasecmp($childTable, $baseTable) === 0) {
            return;
        }

        // Skip if child table doesn't exist
        // If it does, ensure query case matches found case
        $tables = DB::get_schema()->tableList();
        if (!array_key_exists(strtolower($childTable), $tables)) {
            return;
        }
        $childTable = $tables[strtolower($childTable)];

        // Select all orphaned version records
        $orphanedQuery = SQLSelect::create()
            ->selectField("\"{$childTable}\".\"ID\"")
            ->setFrom("\"{$childTable}\"");

        // If we have a parent table limit orphaned records
        // to only those that exist in this
        if (array_key_exists(strtolower($baseTable), $tables)) {
            // Ensure we match db table case
            $baseTable = $tables[strtolower($baseTable)];
            $orphanedQuery
                ->addLeftJoin(
                    $baseTable,
                    "\"{$childTable}\".\"RecordID\" = \"{$baseTable}\".\"RecordID\"
					AND \"{$childTable}\".\"Version\" = \"{$baseTable}\".\"Version\""
                )
                ->addWhere("\"{$baseTable}\".\"ID\" IS NULL");
        }

        $count = $orphanedQuery->count();
        if ($count > 0) {
            DB::alteration_message("Removing {$count} orphaned versioned records", "deleted");
            $ids = $orphanedQuery->execute()->column();
            foreach ($ids as $id) {
                DB::prepared_query("DELETE FROM \"{$childTable}\" WHERE \"ID\" = ?", [$id]);
            }
        }
    }
protected function cleanupVersionedOrphans($baseTable, $childTable)
    {
        // Avoid if disabled
        if ($this->owner->config()->get('versioned_orphans_disabled')) {
            return;
        }

        // Skip if tables are the same (ignore case)
        if (strcasecmp($childTable, $baseTable) === 0) {
            return;
        }

        // Skip if child table doesn't exist
        // If it does, ensure query case matches found case
        $tables = DB::get_schema()->tableList();
        if (!array_key_exists(strtolower($childTable), $tables)) {
            return;
        }
        $childTable = $tables[strtolower($childTable)];

        // Select all orphaned version records
        $orphanedQuery = SQLSelect::create()
            ->selectField("\"{$childTable}\".\"ID\"")
            ->setFrom("\"{$childTable}\"");

        // If we have a parent table limit orphaned records
        // to only those that exist in this
        if (array_key_exists(strtolower($baseTable), $tables)) {
            // Ensure we match db table case
            $baseTable = $tables[strtolower($baseTable)];
            $orphanedQuery
                ->addLeftJoin(
                    $baseTable,
                    "\"{$childTable}\".\"RecordID\" = \"{$baseTable}\".\"RecordID\"
					AND \"{$childTable}\".\"Version\" = \"{$baseTable}\".\"Version\""
                )
                ->addWhere("\"{$baseTable}\".\"ID\" IS NULL");
        }

        $count = $orphanedQuery->count();
        if ($count > 0) {
            DB::alteration_message("Removing {$count} orphaned versioned records", "deleted");
            $ids = $orphanedQuery->execute()->column();
            foreach ($ids as $id) {
                DB::prepared_query("DELETE FROM \"{$childTable}\" WHERE \"ID\" = ?", [$id]);
            }
        }
    }
protected function cleanupVersionedOrphans ( $ baseTable , $ childTable ) { // Avoid if disabled if ( $ this -> owner -> config ( ) -> get ( 'versioned_orphans_disabled' ) ) { return ; } // Skip if tables are the same (ignore case) if ( strcasecmp ( $ childTable , $ baseTable ) === 0 ) { return ; } // Skip if child table doesn't exist // If it does, ensure query case matches found case $ tables = DB :: get_schema ( ) -> tableList ( ) ; if ( ! array_key_exists ( strtolower ( $ childTable ) , $ tables ) ) { return ; } $ childTable = $ tables [ strtolower ( $ childTable ) ] ; // Select all orphaned version records $ orphanedQuery = SQLSelect :: create ( ) -> selectField ( "\"{$childTable}\".\"ID\"" ) -> setFrom ( "\"{$childTable}\"" ) ; // If we have a parent table limit orphaned records // to only those that exist in this if ( array_key_exists ( strtolower ( $ baseTable ) , $ tables ) ) { // Ensure we match db table case $ baseTable = $ tables [ strtolower ( $ baseTable ) ] ; $ orphanedQuery -> addLeftJoin ( $ baseTable , "\"{$childTable}\".\"RecordID\" = \"{$baseTable}\".\"RecordID\"
					AND \"{$childTable}\".\"Version\" = \"{$baseTable}\".\"Version\"" ) -> addWhere ( "\"{$baseTable}\".\"ID\" IS NULL" ) ; } $ count = $ orphanedQuery -> count ( ) ; if ( $ count > 0 ) { DB :: alteration_message ( "Removing {$count} orphaned versioned records" , "deleted" ) ; $ ids = $ orphanedQuery -> execute ( ) -> column ( ) ; foreach ( $ ids as $ id ) { DB :: prepared_query ( "DELETE FROM \"{$childTable}\" WHERE \"ID\" = ?" , [ $ id ] ) ; } } }
Cleanup orphaned records in the _Versions table

@param string $baseTable base table to use as authoritative source of records
@param string $childTable Sub-table to clean orphans from
Cleanup orphaned records in the _Versions table