class EntityGenerator (View source)

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');

Constants

FIELD_VISIBLE_PROTECTED

Specifies class fields should be protected.

FIELD_VISIBLE_PRIVATE

Specifies class fields should be private.

Properties

protected bool $backupExisting
protected string $extension The extension to use for written php files.
protected boolean $isNew Whether or not the current ClassMetadataInfo instance is new or old.
protected array $staticReflection
protected $numSpaces Number of spaces to use for indention in generated code.
protected string $spaces The actual spaces to use for indention.
protected string $classToExtend The class all generated entities should extend.
protected boolean $generateAnnotations Whether or not to generation annotations.
protected string $annotationsPrefix
protected boolean $generateEntityStubMethods Whether or not to generate sub methods.
protected boolean $updateEntityIfExists Whether or not to update the entity class if it exists already.
protected boolean $regenerateEntityIfExists Whether or not to re-generate entity class if it exists already.
protected string $fieldVisibility Visibility of the field
protected boolean. $embeddablesImmutable Whether or not to make generated embeddables immutable.
protected array $typeAlias Hash-map for handle types.
static protected array $generatorStrategyMap Hash-map to handle generator types string.
static protected array $changeTrackingPolicyMap Hash-map to handle the change tracking policy string.
static protected array $inheritanceTypeMap Hash-map to handle the inheritance type string.
static protected string $classTemplate
static protected string $getMethodTemplate
static protected string $setMethodTemplate
static protected string $addMethodTemplate
static protected string $removeMethodTemplate
static protected string $lifecycleCallbackMethodTemplate
static protected string $constructorMethodTemplate
static protected string $embeddableConstructorMethodTemplate

Methods

__construct()

Constructor.

void
generate(array $metadatas, string $outputDirectory)

Generates and writes entity classes for the given array of ClassMetadataInfo instances.

void
writeEntityClass(ClassMetadataInfo $metadata, string $outputDirectory)

Generates and writes entity class to disk for the given ClassMetadataInfo instance.

string
generateEntityClass(ClassMetadataInfo $metadata)

Generates a PHP5 Doctrine 2 entity class from the given ClassMetadataInfo instance.

string
generateUpdatedEntityClass(ClassMetadataInfo $metadata, string $path)

Generates the updated code for the given ClassMetadataInfo and entity at path.

void
setNumSpaces(integer $numSpaces)

Sets the number of spaces the exported class should have.

void
setExtension(string $extension)

Sets the extension to use when writing php files to disk.

void
setClassToExtend(string $classToExtend)

Sets the name of the class the generated classes should extend from.

void
setGenerateAnnotations(bool $bool)

Sets whether or not to generate annotations for the entity.

void
setFieldVisibility(bool $visibility)

Sets the class fields visibility for the entity (can either be private or protected).

setEmbeddablesImmutable(boolean $embeddablesImmutable)

Sets whether or not to generate immutable embeddables.

void
setAnnotationPrefix(string $prefix)

Sets an annotation prefix.

void
setUpdateEntityIfExists(bool $bool)

Sets whether or not to try and update the entity if it already exists.

void
setRegenerateEntityIfExists(bool $bool)

Sets whether or not to regenerate the entity if it exists.

void
setGenerateStubMethods(bool $bool)

Sets whether or not to generate stub methods for the entity.

void
setBackupExisting(bool $bool)

Should an existing entity be backed up if it already exists?

string
getType(string $type)

No description

string
generateEntityNamespace(ClassMetadataInfo $metadata)

No description

string
generateEntityUse()

No description

string
generateEntityClassName(ClassMetadataInfo $metadata)

No description

string
generateEntityBody(ClassMetadataInfo $metadata)

No description

string
generateEntityConstructor(ClassMetadataInfo $metadata)

No description

void
parseTokensInEntityFile(string $src)

No description

bool
hasProperty(string $property, ClassMetadataInfo $metadata)

No description

bool
hasMethod(string $method, ClassMetadataInfo $metadata)

No description

array
getTraits(ClassMetadataInfo $metadata)

No description

bool
hasNamespace(ClassMetadataInfo $metadata)

No description

bool
extendsClass()

No description

string
getClassToExtend()

No description

string
getClassToExtendName()

No description

string
getClassName(ClassMetadataInfo $metadata)

No description

string
getNamespace(ClassMetadataInfo $metadata)

No description

string
generateEntityDocBlock(ClassMetadataInfo $metadata)

No description

string
generateEntityAnnotation(ClassMetadataInfo $metadata)

No description

string
generateTableAnnotation(ClassMetadataInfo $metadata)

No description

string
generateTableConstraints(string $constraintName, array $constraints)

No description

string
string
string
string
generateEntityStubMethods(ClassMetadataInfo $metadata)

No description

bool
isAssociationIsNullable(array $associationMapping)

No description

string
string
string
generateEntityStubMethod(ClassMetadataInfo $metadata, string $type, string $fieldName, string|null $typeHint = null, string|null $defaultValue = null)

No description

string
generateLifecycleCallbackMethod(string $name, string $methodName, ClassMetadataInfo $metadata)

No description

string
generateJoinColumnAnnotation(array $joinColumn)

No description

string
generateAssociationMappingPropertyDocBlock(array $associationMapping, ClassMetadataInfo $metadata)

No description

string
generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata)

No description

string
generateEmbeddedPropertyDocBlock(array $embeddedClass)

No description

string
prefixCodeWithSpaces(string $code, int $num = 1)

No description

string
getInheritanceTypeString(integer $type)

No description

string
getChangeTrackingPolicyString(integer $type)

No description

string
getIdGeneratorTypeString(integer $type)

No description

Details

__construct()

Constructor.

void generate(array $metadatas, string $outputDirectory)

Generates and writes entity classes for the given array of ClassMetadataInfo instances.

Parameters

array $metadatas
string $outputDirectory

Return Value

void

void writeEntityClass(ClassMetadataInfo $metadata, string $outputDirectory)

Generates and writes entity class to disk for the given ClassMetadataInfo instance.

Parameters

ClassMetadataInfo $metadata
string $outputDirectory

Return Value

void

Exceptions

RuntimeException

string generateEntityClass(ClassMetadataInfo $metadata)

Generates a PHP5 Doctrine 2 entity class from the given ClassMetadataInfo instance.

Parameters

ClassMetadataInfo $metadata

Return Value

string

string generateUpdatedEntityClass(ClassMetadataInfo $metadata, string $path)

Generates the updated code for the given ClassMetadataInfo and entity at path.

Parameters

ClassMetadataInfo $metadata
string $path

Return Value

string

void setNumSpaces(integer $numSpaces)

Sets the number of spaces the exported class should have.

Parameters

integer $numSpaces

Return Value

void

void setExtension(string $extension)

Sets the extension to use when writing php files to disk.

Parameters

string $extension

Return Value

void

void setClassToExtend(string $classToExtend)

Sets the name of the class the generated classes should extend from.

Parameters

string $classToExtend

Return Value

void

void setGenerateAnnotations(bool $bool)

Sets whether or not to generate annotations for the entity.

Parameters

bool $bool

Return Value

void

void setFieldVisibility(bool $visibility)

Sets the class fields visibility for the entity (can either be private or protected).

Parameters

bool $visibility

Return Value

void

Exceptions

InvalidArgumentException

setEmbeddablesImmutable(boolean $embeddablesImmutable)

Sets whether or not to generate immutable embeddables.

Parameters

boolean $embeddablesImmutable

void setAnnotationPrefix(string $prefix)

Sets an annotation prefix.

Parameters

string $prefix

Return Value

void

void setUpdateEntityIfExists(bool $bool)

Sets whether or not to try and update the entity if it already exists.

Parameters

bool $bool

Return Value

void

void setRegenerateEntityIfExists(bool $bool)

Sets whether or not to regenerate the entity if it exists.

Parameters

bool $bool

Return Value

void

void setGenerateStubMethods(bool $bool)

Sets whether or not to generate stub methods for the entity.

Parameters

bool $bool

Return Value

void

void setBackupExisting(bool $bool)

Should an existing entity be backed up if it already exists?

Parameters

bool $bool

Return Value

void

protected string getType(string $type)

Parameters

string $type

Return Value

string

protected string generateEntityNamespace(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityUse()

Return Value

string

protected string generateEntityClassName(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityBody(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityConstructor(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected void parseTokensInEntityFile(string $src)

Parameters

string $src

Return Value

void

protected bool hasProperty(string $property, ClassMetadataInfo $metadata)

Parameters

string $property
ClassMetadataInfo $metadata

Return Value

bool

protected bool hasMethod(string $method, ClassMetadataInfo $metadata)

Parameters

string $method
ClassMetadataInfo $metadata

Return Value

bool

protected array getTraits(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

array

Exceptions

ReflectionException

protected bool hasNamespace(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

bool

protected bool extendsClass()

Return Value

bool

protected string getClassToExtend()

Return Value

string

protected string getClassToExtendName()

Return Value

string

protected string getClassName(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string getNamespace(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityDocBlock(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityAnnotation(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateTableAnnotation(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateTableConstraints(string $constraintName, array $constraints)

Parameters

string $constraintName
array $constraints

Return Value

string

protected string generateInheritanceAnnotation(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateDiscriminatorColumnAnnotation(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateDiscriminatorMapAnnotation(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityStubMethods(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected bool isAssociationIsNullable(array $associationMapping)

Parameters

array $associationMapping

Return Value

bool

protected string generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityAssociationMappingProperties(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityFieldMappingProperties(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityEmbeddedProperties(ClassMetadataInfo $metadata)

Parameters

ClassMetadataInfo $metadata

Return Value

string

protected string generateEntityStubMethod(ClassMetadataInfo $metadata, string $type, string $fieldName, string|null $typeHint = null, string|null $defaultValue = null)

Parameters

ClassMetadataInfo $metadata
string $type
string $fieldName
string|null $typeHint
string|null $defaultValue

Return Value

string

protected string generateLifecycleCallbackMethod(string $name, string $methodName, ClassMetadataInfo $metadata)

Parameters

string $name
string $methodName
ClassMetadataInfo $metadata

Return Value

string

protected string generateJoinColumnAnnotation(array $joinColumn)

Parameters

array $joinColumn

Return Value

string

protected string generateAssociationMappingPropertyDocBlock(array $associationMapping, ClassMetadataInfo $metadata)

Parameters

array $associationMapping
ClassMetadataInfo $metadata

Return Value

string

protected string generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata)

Parameters

array $fieldMapping
ClassMetadataInfo $metadata

Return Value

string

protected string generateEmbeddedPropertyDocBlock(array $embeddedClass)

Parameters

array $embeddedClass

Return Value

string

protected string prefixCodeWithSpaces(string $code, int $num = 1)

Parameters

string $code
int $num

Return Value

string

protected string getInheritanceTypeString(integer $type)

Parameters

integer $type The inheritance type used by the class and its subclasses.

Return Value

string The literal string for the inheritance type.

Exceptions

InvalidArgumentException When the inheritance type does not exist.

protected string getChangeTrackingPolicyString(integer $type)

Parameters

integer $type The policy used for change-tracking for the mapped class.

Return Value

string The literal string for the change-tracking type.

Exceptions

InvalidArgumentException When the change-tracking type does not exist.

protected string getIdGeneratorTypeString(integer $type)

Parameters

integer $type The generator to use for the mapped class.

Return Value

string The literal string for the generator type.

Exceptions

InvalidArgumentException When the generator type does not exist.