Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: give Versioned::stageTable a static partner function so you can call it from anywhere. #436

Open
4 tasks done
sunnysideup opened this issue Feb 7, 2024 · 0 comments

Comments

@sunnysideup
Copy link

sunnysideup commented Feb 7, 2024

Description

I sometimes write hacky code like this:

        $tableName = 'Product';
        $stage = Versioned::get_stage();
        if ($stage === 'Live') {
            $tableName .= '_Live';
        }

The "proper" way to write this would be:

    $sng = DataObject::singleton(Product::class);
    $schema = $sng->getSchema();
    $tableName = $schema->tableName(Product::class);
    $tableName = $sng->stageTable($tableName, Versioned::get_stage());

It would be nice if I could write:

$tableName = Versioned::get_stage_table(Product::class); 

This means that we should take stageTable from Versioned and add a static function:
https://github.com/silverstripe/silverstripe-versioned/blob/2/src/Versioned.php#L2125-L2131

public function stageTable(?string $table = '', ?string $stage = '')
{ 
    if(! $tableName) {
        $schema = $sng->getSchema();
        $table = $schema->tableName(get_class($this->getOwner()));
    }
    if($this->hasStages()) {
        if(! $stage) {
            $stage = self::get_stage();
        }    
        if ($this->hasStages() && $stage === static::LIVE) {
            return "{$table}_{$stage}";
        }
    }
    return $table;
}

public static function get_stage_table_from_class_name(string $className, ?string $stage = '')
{
    $obj = Injector::inst()->get($className);
    $tableName = $obj->getSchema()->tableName($className);
    return $obj->stageTable($tableName, $stage); 
}

Additional context or points of discussion

No response

Validations

  • You intend to implement the feature yourself
  • You have read the contributing guide
  • You strongly believe this feature should be in core, rather than being its own community module
  • You have checked for existing issues or pull requests related to this feature (and didn't find any)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants