EntityGenerator
in package
Generic class used to generate PHP5 entity classes from ClassMetadataInfo instances.
[php] $classes = $em->getClassMetadataFactory()->getAllMetadata();
$generator = new \Doctrine\ORM\Tools\EntityGenerator(); $generator->setGenerateAnnotations(true); $generator->setGenerateStubMethods(true); $generator->setRegenerateEntityIfExists(false); $generator->setUpdateEntityIfExists(true); $generator->generate($classes, '/path/to/generate/entities');
Tags
Table of Contents
Constants
- FIELD_VISIBLE_PRIVATE = 'private'
- Specifies class fields should be private.
- FIELD_VISIBLE_PROTECTED = 'protected'
- Specifies class fields should be protected.
Properties
- $addMethodTemplate : string
- $annotationsPrefix : string
- $backupExisting : bool
- $changeTrackingPolicyMap : mixed
- Hash-map to handle the change tracking policy string.
- $classTemplate : string
- $classToExtend : string
- The class all generated entities should extend.
- $constructorMethodTemplate : string
- $embeddableConstructorMethodTemplate : string
- $embeddablesImmutable : bool
- Whether or not to make generated embeddables immutable.
- $extension : string
- The extension to use for written php files.
- $fieldVisibility : string
- Visibility of the field
- $generateAnnotations : bool
- Whether or not to generation annotations.
- $generateEntityStubMethods : bool
- Whether or not to generate sub methods.
- $generatorStrategyMap : mixed
- Hash-map to handle generator types string.
- $getMethodTemplate : string
- $inflector : Inflector
- $inheritanceTypeMap : mixed
- Hash-map to handle the inheritance type string.
- $isNew : bool
- Whether or not the current ClassMetadataInfo instance is new or old.
- $lifecycleCallbackMethodTemplate : string
- $numSpaces : int
- Number of spaces to use for indention in generated code.
- $regenerateEntityIfExists : bool
- Whether or not to re-generate entity class if it exists already.
- $removeMethodTemplate : string
- $setMethodTemplate : string
- $spaces : string
- The actual spaces to use for indention.
- $staticReflection : array<string|int, mixed>
- $typeAlias : mixed
- Hash-map for handle types.
- $updateEntityIfExists : bool
- Whether or not to update the entity class if it exists already.
Methods
- __construct() : mixed
- generate() : void
- Generates and writes entity classes for the given array of ClassMetadataInfo instances.
- generateEntityClass() : string
- Generates a PHP5 Doctrine 2 entity class from the given ClassMetadataInfo instance.
- generateUpdatedEntityClass() : string
- Generates the updated code for the given ClassMetadataInfo and entity at path.
- setAnnotationPrefix() : void
- Sets an annotation prefix.
- setBackupExisting() : void
- Should an existing entity be backed up if it already exists?
- setClassToExtend() : void
- Sets the name of the class the generated classes should extend from.
- setEmbeddablesImmutable() : void
- Sets whether or not to generate immutable embeddables.
- setExtension() : void
- Sets the extension to use when writing php files to disk.
- setFieldVisibility() : void
- Sets the class fields visibility for the entity (can either be private or protected).
- setGenerateAnnotations() : void
- Sets whether or not to generate annotations for the entity.
- setGenerateStubMethods() : void
- Sets whether or not to generate stub methods for the entity.
- setInflector() : void
- setNumSpaces() : void
- Sets the number of spaces the exported class should have.
- setRegenerateEntityIfExists() : void
- Sets whether or not to regenerate the entity if it exists.
- setUpdateEntityIfExists() : void
- Sets whether or not to try and update the entity if it already exists.
- writeEntityClass() : void
- Generates and writes entity class to disk for the given ClassMetadataInfo instance.
- extendsClass() : bool
- generateAssociationMappingPropertyDocBlock() : string
- generateDiscriminatorColumnAnnotation() : string
- generateDiscriminatorMapAnnotation() : string|null
- generateEmbeddedPropertyDocBlock() : string
- generateEntityAnnotation() : string
- generateEntityAssociationMappingProperties() : string
- generateEntityBody() : string
- generateEntityClassName() : string
- generateEntityConstructor() : string
- generateEntityDocBlock() : string
- generateEntityEmbeddedProperties() : string
- generateEntityFieldMappingProperties() : string
- generateEntityLifecycleCallbackMethods() : string
- generateEntityNamespace() : string
- generateEntityStubMethod() : string
- generateEntityStubMethods() : string
- generateEntityUse() : string
- generateFieldMappingPropertyDocBlock() : string
- generateInheritanceAnnotation() : string
- generateJoinColumnAnnotation() : string
- generateLifecycleCallbackMethod() : string
- generateTableAnnotation() : string
- generateTableConstraints() : string
- getChangeTrackingPolicyString() : string
- getClassName() : string
- getClassToExtend() : string
- getClassToExtendName() : string
- getIdGeneratorTypeString() : string
- getInheritanceTypeString() : string
- getNamespace() : string
- getTraits() : array<string|int, ReflectionClass>
- getType() : string
- hasMethod() : bool
- hasNamespace() : bool
- hasProperty() : bool
- isAssociationIsNullable() : bool
- parseTokensInEntityFile() : void
- prefixCodeWithSpaces() : string
- exportTableOptions() : string
- Exports (nested) option elements.
- generateEmbeddableConstructor() : string
- generateEntityListenerAnnotation() : string
- nullableFieldExpression() : string|null
Constants
FIELD_VISIBLE_PRIVATE
Specifies class fields should be private.
public
mixed
FIELD_VISIBLE_PRIVATE
= 'private'
FIELD_VISIBLE_PROTECTED
Specifies class fields should be protected.
public
mixed
FIELD_VISIBLE_PROTECTED
= 'protected'
Properties
$addMethodTemplate
protected
static string
$addMethodTemplate
= '/**
* <description>
*
* @param <variableType> $<variableName>
*
* @return <entity>
*/
public function <methodName>(<methodTypeHint>$<variableName>)
{
<spaces>$this-><fieldName>[] = $<variableName>;
<spaces>return $this;
}'
$annotationsPrefix
protected
string
$annotationsPrefix
= ''
$backupExisting
protected
bool
$backupExisting
= true
$changeTrackingPolicyMap
Hash-map to handle the change tracking policy string.
protected
static mixed
$changeTrackingPolicyMap
= [\Doctrine\ORM\Mapping\ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT => 'DEFERRED_IMPLICIT', \Doctrine\ORM\Mapping\ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT => 'DEFERRED_EXPLICIT', \Doctrine\ORM\Mapping\ClassMetadataInfo::CHANGETRACKING_NOTIFY => 'NOTIFY']
Tags
$classTemplate
protected
static string
$classTemplate
= '<?php
<namespace>
<useStatement>
<entityAnnotation>
<entityClassName>
{
<entityBody>
}
'
$classToExtend
The class all generated entities should extend.
protected
string
$classToExtend
$constructorMethodTemplate
protected
static string
$constructorMethodTemplate
= '/**
* Constructor
*/
public function __construct()
{
<spaces><collections>
}
'
$embeddableConstructorMethodTemplate
protected
static string
$embeddableConstructorMethodTemplate
= '/**
* Constructor
*
* <paramTags>
*/
public function __construct(<params>)
{
<spaces><fields>
}
'
$embeddablesImmutable
Whether or not to make generated embeddables immutable.
protected
bool
$embeddablesImmutable
= false
$extension
The extension to use for written php files.
protected
string
$extension
= '.php'
$fieldVisibility
Visibility of the field
protected
string
$fieldVisibility
= 'private'
$generateAnnotations
Whether or not to generation annotations.
protected
bool
$generateAnnotations
= false
$generateEntityStubMethods
Whether or not to generate sub methods.
protected
bool
$generateEntityStubMethods
= false
$generatorStrategyMap
Hash-map to handle generator types string.
protected
static mixed
$generatorStrategyMap
= [\Doctrine\ORM\Mapping\ClassMetadataInfo::GENERATOR_TYPE_AUTO => 'AUTO', \Doctrine\ORM\Mapping\ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE => 'SEQUENCE', \Doctrine\ORM\Mapping\ClassMetadataInfo::GENERATOR_TYPE_IDENTITY => 'IDENTITY', \Doctrine\ORM\Mapping\ClassMetadataInfo::GENERATOR_TYPE_NONE => 'NONE', \Doctrine\ORM\Mapping\ClassMetadataInfo::GENERATOR_TYPE_UUID => 'UUID', \Doctrine\ORM\Mapping\ClassMetadataInfo::GENERATOR_TYPE_CUSTOM => 'CUSTOM']
Tags
$getMethodTemplate
protected
static string
$getMethodTemplate
= '/**
* <description>
*
* @return <variableType>
*/
public function <methodName>()
{
<spaces>return $this-><fieldName>;
}'
$inflector
protected
Inflector
$inflector
$inheritanceTypeMap
Hash-map to handle the inheritance type string.
protected
static mixed
$inheritanceTypeMap
= [\Doctrine\ORM\Mapping\ClassMetadataInfo::INHERITANCE_TYPE_NONE => 'NONE', \Doctrine\ORM\Mapping\ClassMetadataInfo::INHERITANCE_TYPE_JOINED => 'JOINED', \Doctrine\ORM\Mapping\ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE => 'SINGLE_TABLE', \Doctrine\ORM\Mapping\ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS => 'TABLE_PER_CLASS']
Tags
$isNew
Whether or not the current ClassMetadataInfo instance is new or old.
protected
bool
$isNew
= true
$lifecycleCallbackMethodTemplate
protected
static string
$lifecycleCallbackMethodTemplate
= '/**
* @<name>
*/
public function <methodName>()
{
<spaces>// Add your code here
}'
$numSpaces
Number of spaces to use for indention in generated code.
protected
int
$numSpaces
= 4
$regenerateEntityIfExists
Whether or not to re-generate entity class if it exists already.
protected
bool
$regenerateEntityIfExists
= false
$removeMethodTemplate
protected
static string
$removeMethodTemplate
= '/**
* <description>
*
* @param <variableType> $<variableName>
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function <methodName>(<methodTypeHint>$<variableName>)
{
<spaces>return $this-><fieldName>->removeElement($<variableName>);
}'
$setMethodTemplate
protected
static string
$setMethodTemplate
= '/**
* <description>
*
* @param <variableType> $<variableName>
*
* @return <entity>
*/
public function <methodName>(<methodTypeHint>$<variableName><variableDefault>)
{
<spaces>$this-><fieldName> = $<variableName>;
<spaces>return $this;
}'
$spaces
The actual spaces to use for indention.
protected
string
$spaces
= ' '
$staticReflection
protected
array<string|int, mixed>
$staticReflection
= []
$typeAlias
Hash-map for handle types.
protected
mixed
$typeAlias
= [\Doctrine\DBAL\Types\Types::DATETIMETZ_MUTABLE => '\\DateTime', \Doctrine\DBAL\Types\Types::DATETIME_MUTABLE => '\\DateTime', \Doctrine\DBAL\Types\Types::DATE_MUTABLE => '\\DateTime', \Doctrine\DBAL\Types\Types::TIME_MUTABLE => '\\DateTime', \Doctrine\DBAL\Types\Types::OBJECT => '\\stdClass', \Doctrine\DBAL\Types\Types::INTEGER => 'int', \Doctrine\DBAL\Types\Types::BIGINT => 'int', \Doctrine\DBAL\Types\Types::SMALLINT => 'int', \Doctrine\DBAL\Types\Types::TEXT => 'string', \Doctrine\DBAL\Types\Types::BLOB => 'string', \Doctrine\DBAL\Types\Types::DECIMAL => 'string', \Doctrine\DBAL\Types\Types::GUID => 'string', 'json_array' => 'array', \Doctrine\DBAL\Types\Types::JSON => 'array', \Doctrine\DBAL\Types\Types::SIMPLE_ARRAY => 'array', \Doctrine\DBAL\Types\Types::BOOLEAN => 'bool']
Tags
$updateEntityIfExists
Whether or not to update the entity class if it exists already.
protected
bool
$updateEntityIfExists
= false
Methods
__construct()
public
__construct() : mixed
generate()
Generates and writes entity classes for the given array of ClassMetadataInfo instances.
public
generate(array<string|int, mixed> $metadatas, string $outputDirectory) : void
Parameters
- $metadatas : array<string|int, mixed>
- $outputDirectory : string
Tags
generateEntityClass()
Generates a PHP5 Doctrine 2 entity class from the given ClassMetadataInfo instance.
public
generateEntityClass(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateUpdatedEntityClass()
Generates the updated code for the given ClassMetadataInfo and entity at path.
public
generateUpdatedEntityClass(ClassMetadataInfo $metadata, string $path) : string
Parameters
- $metadata : ClassMetadataInfo
- $path : string
Return values
stringsetAnnotationPrefix()
Sets an annotation prefix.
public
setAnnotationPrefix(string $prefix) : void
Parameters
- $prefix : string
setBackupExisting()
Should an existing entity be backed up if it already exists?
public
setBackupExisting(bool $bool) : void
Parameters
- $bool : bool
setClassToExtend()
Sets the name of the class the generated classes should extend from.
public
setClassToExtend(string $classToExtend) : void
Parameters
- $classToExtend : string
setEmbeddablesImmutable()
Sets whether or not to generate immutable embeddables.
public
setEmbeddablesImmutable(bool $embeddablesImmutable) : void
Parameters
- $embeddablesImmutable : bool
setExtension()
Sets the extension to use when writing php files to disk.
public
setExtension(string $extension) : void
Parameters
- $extension : string
setFieldVisibility()
Sets the class fields visibility for the entity (can either be private or protected).
public
setFieldVisibility(string $visibility) : void
Parameters
- $visibility : string
Tags
setGenerateAnnotations()
Sets whether or not to generate annotations for the entity.
public
setGenerateAnnotations(bool $bool) : void
Parameters
- $bool : bool
setGenerateStubMethods()
Sets whether or not to generate stub methods for the entity.
public
setGenerateStubMethods(bool $bool) : void
Parameters
- $bool : bool
setInflector()
public
setInflector(Inflector $inflector) : void
Parameters
- $inflector : Inflector
setNumSpaces()
Sets the number of spaces the exported class should have.
public
setNumSpaces(int $numSpaces) : void
Parameters
- $numSpaces : int
setRegenerateEntityIfExists()
Sets whether or not to regenerate the entity if it exists.
public
setRegenerateEntityIfExists(bool $bool) : void
Parameters
- $bool : bool
setUpdateEntityIfExists()
Sets whether or not to try and update the entity if it already exists.
public
setUpdateEntityIfExists(bool $bool) : void
Parameters
- $bool : bool
writeEntityClass()
Generates and writes entity class to disk for the given ClassMetadataInfo instance.
public
writeEntityClass(ClassMetadataInfo $metadata, string $outputDirectory) : void
Parameters
- $metadata : ClassMetadataInfo
- $outputDirectory : string
Tags
extendsClass()
protected
extendsClass() : bool
Return values
boolgenerateAssociationMappingPropertyDocBlock()
protected
generateAssociationMappingPropertyDocBlock(array<string|int, mixed> $associationMapping, ClassMetadataInfo $metadata) : string
Parameters
- $associationMapping : array<string|int, mixed>
- $metadata : ClassMetadataInfo
Return values
stringgenerateDiscriminatorColumnAnnotation()
protected
generateDiscriminatorColumnAnnotation(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateDiscriminatorMapAnnotation()
protected
generateDiscriminatorMapAnnotation(ClassMetadataInfo $metadata) : string|null
Parameters
- $metadata : ClassMetadataInfo
Return values
string|nullgenerateEmbeddedPropertyDocBlock()
protected
generateEmbeddedPropertyDocBlock(array<string|int, mixed> $embeddedClass) : string
Parameters
- $embeddedClass : array<string|int, mixed>
Tags
Return values
stringgenerateEntityAnnotation()
protected
generateEntityAnnotation(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityAssociationMappingProperties()
protected
generateEntityAssociationMappingProperties(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityBody()
protected
generateEntityBody(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityClassName()
protected
generateEntityClassName(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityConstructor()
protected
generateEntityConstructor(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityDocBlock()
protected
generateEntityDocBlock(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityEmbeddedProperties()
protected
generateEntityEmbeddedProperties(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityFieldMappingProperties()
protected
generateEntityFieldMappingProperties(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityLifecycleCallbackMethods()
protected
generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityNamespace()
protected
generateEntityNamespace(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityStubMethod()
protected
generateEntityStubMethod(ClassMetadataInfo $metadata, string $type, string $fieldName[, string|null $typeHint = null ][, string|null $defaultValue = null ]) : string
Parameters
- $metadata : ClassMetadataInfo
- $type : string
- $fieldName : string
- $typeHint : string|null = null
- $defaultValue : string|null = null
Return values
stringgenerateEntityStubMethods()
protected
generateEntityStubMethods(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityUse()
protected
generateEntityUse() : string
Return values
stringgenerateFieldMappingPropertyDocBlock()
protected
generateFieldMappingPropertyDocBlock(array<string|int, mixed> $fieldMapping, ClassMetadataInfo $metadata) : string
Parameters
- $fieldMapping : array<string|int, mixed>
- $metadata : ClassMetadataInfo
Return values
stringgenerateInheritanceAnnotation()
protected
generateInheritanceAnnotation(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateJoinColumnAnnotation()
protected
generateJoinColumnAnnotation(array<string|int, mixed> $joinColumn) : string
Parameters
- $joinColumn : array<string|int, mixed>
Tags
Return values
stringgenerateLifecycleCallbackMethod()
protected
generateLifecycleCallbackMethod(string $name, string $methodName, ClassMetadataInfo $metadata) : string
Parameters
- $name : string
- $methodName : string
- $metadata : ClassMetadataInfo
Return values
stringgenerateTableAnnotation()
protected
generateTableAnnotation(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateTableConstraints()
protected
generateTableConstraints(string $constraintName, array<string|int, mixed> $constraints) : string
Parameters
- $constraintName : string
- $constraints : array<string|int, mixed>
Tags
Return values
stringgetChangeTrackingPolicyString()
protected
getChangeTrackingPolicyString(int $type) : string
Parameters
- $type : int
-
The policy used for change-tracking for the mapped class.
Tags
Return values
string —The literal string for the change-tracking type.
getClassName()
protected
getClassName(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgetClassToExtend()
protected
getClassToExtend() : string
Return values
stringgetClassToExtendName()
protected
getClassToExtendName() : string
Return values
stringgetIdGeneratorTypeString()
protected
getIdGeneratorTypeString(int $type) : string
Parameters
- $type : int
-
The generator to use for the mapped class.
Tags
Return values
string —The literal string for the generator type.
getInheritanceTypeString()
protected
getInheritanceTypeString(int $type) : string
Parameters
- $type : int
-
The inheritance type used by the class and its subclasses.
Tags
Return values
string —The literal string for the inheritance type.
getNamespace()
protected
getNamespace(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgetTraits()
protected
getTraits(ClassMetadataInfo $metadata) : array<string|int, ReflectionClass>
Parameters
- $metadata : ClassMetadataInfo
Tags
Return values
array<string|int, ReflectionClass>getType()
protected
getType(string $type) : string
Parameters
- $type : string
Return values
stringhasMethod()
protected
hasMethod(string $method, ClassMetadataInfo $metadata) : bool
Parameters
- $method : string
- $metadata : ClassMetadataInfo
Return values
boolhasNamespace()
protected
hasNamespace(ClassMetadataInfo $metadata) : bool
Parameters
- $metadata : ClassMetadataInfo
Return values
boolhasProperty()
protected
hasProperty(string $property, ClassMetadataInfo $metadata) : bool
Parameters
- $property : string
- $metadata : ClassMetadataInfo
Return values
boolisAssociationIsNullable()
protected
isAssociationIsNullable(array<string|int, mixed> $associationMapping) : bool
Parameters
- $associationMapping : array<string|int, mixed>
Tags
Return values
boolparseTokensInEntityFile()
protected
parseTokensInEntityFile(string $src) : void
Parameters
- $src : string
Tags
prefixCodeWithSpaces()
protected
prefixCodeWithSpaces(string $code[, int $num = 1 ]) : string
Parameters
- $code : string
- $num : int = 1
Return values
stringexportTableOptions()
Exports (nested) option elements.
private
exportTableOptions(array<string|int, mixed> $options) : string
Parameters
- $options : array<string|int, mixed>
Tags
Return values
stringgenerateEmbeddableConstructor()
private
generateEmbeddableConstructor(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringgenerateEntityListenerAnnotation()
private
generateEntityListenerAnnotation(ClassMetadataInfo $metadata) : string
Parameters
- $metadata : ClassMetadataInfo
Return values
stringnullableFieldExpression()
private
nullableFieldExpression(array<string|int, mixed> $fieldMapping) : string|null
Parameters
- $fieldMapping : array<string|int, mixed>