class Service (View source)

Service class for tasks related to Doctrine

Constants

DOCTRINE_MIGRATIONSTABLENAME

Properties

array $output
protected EntityManagerInterface $entityManager
protected PackageManager $packageManager
protected Environment $environment

Methods

array
validateMapping()

Validates the metadata mapping for Doctrine, using the SchemaValidator of Doctrine.

string
createSchema(string $outputPathAndFilename = null)

Creates the needed DB schema using Doctrine's SchemaTool. If tables already exist, this will throw an exception.

string
updateSchema(bool $safeMode = true, string $outputPathAndFilename = null)

Updates the DB schema using Doctrine's SchemaTool. The $safeMode flag is passed to SchemaTool unchanged.

void
compileProxies()

Compiles the Doctrine proxy class code using the Doctrine ProxyFactory.

array
getEntityStatus()

Returns information about which entities exist and possibly if their mapping information contains errors or not.

mixed
runDql(string $dql, int $hydrationMode = \Doctrine\ORM\Query::HYDRATE_OBJECT, int $firstResult = null, int $maxResult = null)

Run DQL and return the result as-is.

Configuration
getMigrationConfiguration()

Return the configuration needed for Migrations.

array
getMigrationStatus()

Returns the current migration status as an array.

string
getFormattedMigrationStatus(bool $showMigrations = false, bool $showDescriptions = false)

Returns a formatted string of current database migration status.

string
getPackageKeyFromMigrationVersion(Version $version)

Tries to find out a package key which the Version belongs to. If no package could be found, an empty string is returned.

string
getFormattedVersionAlias(string $alias, Configuration $configuration)

Returns a formatted version string for the alias.

string
getMigrationDescription(Version $version, DocCommentParser $parser)

Returns the description of a migration.

string
executeMigrations(string $version = null, string $outputPathAndFilename = null, bool $dryRun = false, bool $quiet = false)

Execute all new migrations, up to $version if given.

string
executeMigration(string $version, string $direction = 'up', string $outputPathAndFilename = null, bool $dryRun = false)

Execute a single migration in up or down direction. If $path is given, the SQL statements will be written to the file in $path instead of executed.

void
markAsMigrated(string $version, bool $markAsMigrated)

Add a migration version to the migrations table or remove it.

array
generateMigration(bool $diffAgainstCurrent = true, string $filterExpression = null)

Generates a new migration file and returns the path to it.

string
writeMigrationClassToFile(Configuration $configuration, string|null $up, string|null $down)

No description

string
buildCodeFromSql(Configuration $configuration, array $sql)

Returns PHP code for a migration file that "executes" the given array of SQL statements.

string
getDatabasePlatformName()

Get name of current database platform

static array
getForeignKeyHandlingSql(Schema $schema, AbstractPlatform $platform, array $tableNames, string $search, string $replace)

This serves a rather strange use case: renaming columns used in FK constraints.

Details

array validateMapping()

Validates the metadata mapping for Doctrine, using the SchemaValidator of Doctrine.

Return Value

array

string createSchema(string $outputPathAndFilename = null)

Creates the needed DB schema using Doctrine's SchemaTool. If tables already exist, this will throw an exception.

Parameters

string $outputPathAndFilename

A file to write SQL to, instead of executing it

Return Value

string

Exceptions

ToolsException

string updateSchema(bool $safeMode = true, string $outputPathAndFilename = null)

Updates the DB schema using Doctrine's SchemaTool. The $safeMode flag is passed to SchemaTool unchanged.

Parameters

bool $safeMode
string $outputPathAndFilename

A file to write SQL to, instead of executing it

Return Value

string

void compileProxies()

Compiles the Doctrine proxy class code using the Doctrine ProxyFactory.

Return Value

void

Exceptions

FilesException

array getEntityStatus()

Returns information about which entities exist and possibly if their mapping information contains errors or not.

Return Value

array

Exceptions

ORMException

mixed runDql(string $dql, int $hydrationMode = \Doctrine\ORM\Query::HYDRATE_OBJECT, int $firstResult = null, int $maxResult = null)

Run DQL and return the result as-is.

Parameters

string $dql
int $hydrationMode
int $firstResult
int $maxResult

Return Value

mixed

protected Configuration getMigrationConfiguration()

Return the configuration needed for Migrations.

Return Value

Configuration

Exceptions

DBALException

array getMigrationStatus()

Returns the current migration status as an array.

Return Value

array

Exceptions

DBALException

string getFormattedMigrationStatus(bool $showMigrations = false, bool $showDescriptions = false)

Returns a formatted string of current database migration status.

Parameters

bool $showMigrations
bool $showDescriptions

Return Value

string

Exceptions

ReflectionException
DBALException

protected string getPackageKeyFromMigrationVersion(Version $version)

Tries to find out a package key which the Version belongs to. If no package could be found, an empty string is returned.

Parameters

Version $version

Return Value

string

Exceptions

ReflectionException

protected string getFormattedVersionAlias(string $alias, Configuration $configuration)

Returns a formatted version string for the alias.

Parameters

string $alias
Configuration $configuration

Return Value

string

protected string getMigrationDescription(Version $version, DocCommentParser $parser)

Returns the description of a migration.

If available it is fetched from the getDescription() method, if that returns an empty value the class docblock is used instead.

Parameters

Version $version
DocCommentParser $parser

Return Value

string

Exceptions

ReflectionException

string executeMigrations(string $version = null, string $outputPathAndFilename = null, bool $dryRun = false, bool $quiet = false)

Execute all new migrations, up to $version if given.

If $outputPathAndFilename is given, the SQL statements will be written to the given file instead of executed.

Parameters

string $version

The version to migrate to

string $outputPathAndFilename

A file to write SQL to, instead of executing it

bool $dryRun

Whether to do a dry run or not

bool $quiet

Whether to do a quiet run or not

Return Value

string

Exceptions

MigrationException
DBALException

string executeMigration(string $version, string $direction = 'up', string $outputPathAndFilename = null, bool $dryRun = false)

Execute a single migration in up or down direction. If $path is given, the SQL statements will be written to the file in $path instead of executed.

Parameters

string $version

The version to migrate to

string $direction
string $outputPathAndFilename

A file to write SQL to, instead of executing it

bool $dryRun

Whether to do a dry run or not

Return Value

string

Exceptions

MigrationException
DBALException

void markAsMigrated(string $version, bool $markAsMigrated)

Add a migration version to the migrations table or remove it.

This does not execute any migration code but simply records a version as migrated or not.

Parameters

string $version

The version to add or remove

bool $markAsMigrated

Return Value

void

Exceptions

MigrationException
LogicException
DBALException

array generateMigration(bool $diffAgainstCurrent = true, string $filterExpression = null)

Generates a new migration file and returns the path to it.

If $diffAgainstCurrent is true, it generates a migration file with the diff between current DB structure and the found mapping metadata.

Only include tables/sequences matching the $filterExpression regexp when diffing models and existing schema.

Otherwise an empty migration skeleton is generated.

Parameters

bool $diffAgainstCurrent
string $filterExpression

Return Value

array

Path to the new file

Exceptions

DBALException
ORMException
FilesException

protected string writeMigrationClassToFile(Configuration $configuration, string|null $up, string|null $down)

No description

Parameters

Configuration $configuration
string|null $up
string|null $down

Return Value

string

Exceptions

RuntimeException
FilesException

protected string buildCodeFromSql(Configuration $configuration, array $sql)

Returns PHP code for a migration file that "executes" the given array of SQL statements.

Parameters

Configuration $configuration
array $sql

Return Value

string

Exceptions

DBALException

string getDatabasePlatformName()

Get name of current database platform

Return Value

string

Exceptions

DBALException

static array getForeignKeyHandlingSql(Schema $schema, AbstractPlatform $platform, array $tableNames, string $search, string $replace)

This serves a rather strange use case: renaming columns used in FK constraints.

For a column that is used in a FK constraint to be renamed, the FK constraint has to be dropped first, then the column can be renamed and last the FK constraint needs to be added back (using the new name, of course).

This method helps with the task of handling the FK constraints during this. Given a list of tables that contain columns to be renamed and a search/replace pair for the column name, it will return an array with arrays with drop and add SQL statements.

Use them like this before and after renaming the affected fields:

// collect foreign keys pointing to "our" tables $tableNames = array(...); $foreignKeyHandlingSql = $this->getForeignKeyHandlingSql($schema, $tableNames, 'old_name', 'new_name');

// drop FK constraints foreach ($foreignKeyHandlingSql['drop'] as $sql) { $this->addSql($sql); }

// rename columns now

// add back FK constraints foreach ($foreignKeyHandlingSql['add'] as $sql) { $this->addSql($sql); }

Parameters

Schema $schema
AbstractPlatform $platform
array $tableNames
string $search
string $replace

Return Value

array