silverstripe / silverstripe-versioned

Versioning for Silverstripe model
BSD 3-Clause "New" or "Revised" License
8 stars 34 forks source link

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

Open sunnysideup opened 9 months ago

sunnysideup commented 9 months ago

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