protected function reorderTables($tables, $relations)
    {
        $depencies = $orders = $result = [];
        foreach ($relations as $table => $relation) {
            if (isset($relation[$table])) {
                unset($relation[$table]);
            }
            $depencies[$table] = array_keys($relation);
        }
        $tableNames = array_keys($tables);
        sort($tableNames);
        $this->reorderRecrusive($tableNames, $depencies, $orders);
        foreach (array_keys($orders) as $value) {
            if (isset($tables[$value])) {
                $result[] = $tables[$value];
            }
        }
        return $result;
    }