Download the PHP package archipro/silverstripe-smart-enum without Composer

On this page you can find all versions of the php package archipro/silverstripe-smart-enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package silverstripe-smart-enum

Silverstripe SmartEnum DBField

Map a PHP 8.1+ BackedEnum to a Silverstripe DataObject database column. Values are derived from enum cases automatically. By default the column uses a database-native ENUM type (MySQL today); set use_native_db_enum to false to use a scalar column (VARCHAR for string-backed enums, INT for int-backed enums) when you need to avoid costly ENUM alters on large tables. This option refers to the database column type, not PHP BackedEnum.

CMS ModelAdmin and DataObject edit forms scaffold a DropdownField with the enum’s backing values (inherited from Silverstripe’s DBEnum).

Installation

Monorepo / path repository

Run composer update archipro/silverstripe-smart-enum and sake dev/build (or your usual schema build).

The module registers a SmartEnum Injector alias for use in $db field specs.

Define a SmartEnum on a DataObject

Use the SmartEnum Injector alias in your $db array. Double-escape backslashes in the enum class name inside the field specification string:

Int-backed enums work the same way; pass the int backing scalar as the default:

Default value

Omit the second argument when the column should have no default. New records start with an empty value until the field is set:

Pass an explicit default as the second argument. Use the backing scalar of the enum case that represents the initial business state (for example the status a new record should start in):

You can also pass null explicitly; that is equivalent to omitting the default:

When building the $db array in PHP (not a string field spec), you may pass a BackedEnum case instead of the scalar.

The default must match a case on the enum. Invalid scalars and enum cases from another type are rejected at field construction time.

Unlike core DBEnum, integer defaults are not treated as list indices. Pass the actual backing value (or an enum case), not a positional index.

Optional: enum class in $db (advanced)

By default this module only registers the SmartEnum Injector alias (see above). That is the recommended approach.

You can opt in to a different syntax that uses the backed enum FQCN directly in $db:

This requires wrapping the Injector config locator at application bootstrap. The decorator composes with whatever locator is already installed (other modules can decorate the chain too).

Preferred (idempotent):

Explicit equivalent:

Copy docs/enum-class-field-spec.bootstrap.php into your app (for example app/_config/smart-enum-enum-class.php). Do not add that file under this module’s _config/ directory.

Behaviour and limitations

Both syntaxes can coexist when enum-class mode is enabled.

MySQL ENUM vs scalar columns

use_native_db_enum String-backed enum Int-backed enum
true (default) MySQL ENUM MySQL ENUM (values stored as quoted ints; coerced on read)
false VARCHAR INT

Use use_native_db_enum: true for smaller schemas with values enforced by the database. Set use_native_db_enum to false on large tables where altering an ENUM is slow or risky, or when you want a native INT column for int-backed enums.

Per-field override in the field spec options (4th argument):

For string-backed enums with use_native_db_enum: false, varchar_length is optional; when omitted, the length is max(50, longest backing value length) capped at 255.

Int-backed enums with use_native_db_enum: true return stringified values from MySQL. DBSmartEnum coerces numeric strings back to int at the field boundary (for example when reading via dbObject() or typed accessors).

YAML / site-wide default

Per-field use_native_db_enum must be set in the field spec options (4th argument) as shown above. To change the default for all SmartEnum fields that omit it, use static config:

Typed accessors (optional)

SmartEnumDataExtension is not applied globally. Add it to each DataObject that uses SmartEnum columns.

YAML:

Or in PHP:

For each DBSmartEnum column Status the extension provides:

If the model already defines getStatus() / setStatus(), those methods take precedence over the extension.

Property access and PHPDoc

The primary use case is property access on the DataObject. When the extension is applied, $record->Status resolves via getStatus() / setStatus() and returns a BackedEnum instance (or null). Property assignment validates the value; invalid backing scalars throw InvalidArgumentException.

Declare the typed property on your model for IDE and static analysis support:

getField('Status') still returns the raw backing scalar stored in the database record, not the enum instance.

CMS forms

No extra configuration is required. scaffoldFormField() returns a DropdownField listing all backing values, whether or not use_native_db_enum is enabled.

Migrating database ENUM → scalar columns on production

Flipping use_native_db_enum from true to false on a live, large table is an operational task. dev/build may issue ALTER TABLE statements that lock or rebuild the table for a long time. Plan a manual migration and maintenance window; do not rely on a casual dev/build on production for column-type changes.

Running tests

composer test requires a reachable MySQL-compatible database. Configure the connection via a project .env file or SS_DATABASE_* environment variables (for example SS_DATABASE_SERVER, SS_DATABASE_USERNAME, SS_DATABASE_PASSWORD, and optionally SS_DATABASE_CHOOSE_NAME=true). The suite fails if the database is missing or unreachable. GitHub Actions sets these automatically via silverstripe/gha-ci.

License

BSD-3-Clause


All versions of silverstripe-smart-enum with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
silverstripe/framework Version ^5.0||^4.13
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package archipro/silverstripe-smart-enum contains the following files

Loading the files please wait ...