class Service (View source)

Service class for tasks related to Doctrine

Constants

DOCTRINE_MIGRATIONSTABLENAME

DOCTRINE_MIGRATIONSNAMESPACE

Properties

protected EntityManagerInterface $entityManager
protected PackageManager $packageManager
protected Environment $environment
protected BufferedOutput $logMessages

Methods

array
validateMapping()

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

void
createSchema(string $outputPathAndFilename = null)

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

void
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.

DependencyFactory
getDependencyFactory()

Return the configuration needed for Migrations.

string
getFormattedMigrationStatus(bool $showMigrations = false)

Returns a formatted string of current database migration status.

string
executeMigrations(string $version = 'latest', 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
getMigrationStatus()

Returns the current migration status as an array.

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

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

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

void 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

void

Exceptions

ToolsException

void 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

void

void compileProxies()

Compiles the Doctrine proxy class code using the Doctrine ProxyFactory.

Return Value

void

Exceptions

FilesException
Exception

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 DependencyFactory getDependencyFactory()

Return the configuration needed for Migrations.

Return Value

DependencyFactory

Exceptions

FilesException

string getFormattedMigrationStatus(bool $showMigrations = false)

Returns a formatted string of current database migration status.

Parameters

bool $showMigrations

Return Value

string

Exceptions

Exception

string executeMigrations(string $version = 'latest', 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 - implicitly enables dry-run

bool $dryRun

Whether to do a dry run or not

bool $quiet

Whether to do a quiet run or not

Return Value

string

Exceptions

Exception

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

Exception

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
Exception

array getMigrationStatus()

Returns the current migration status as an array.

Return Value

array

Exceptions

Exception

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

Exception

string getDatabasePlatformName()

Get name of current database platform

Return Value

string

Exceptions

Exception

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