Generates a Doctrine 2 entity class from the given EntityMetadata instance

@param EntityMetadata $meta
@return string
773366
public function generateEntityClass(EntityMetadata $meta)
    {
        $placeholders = array(
            '<namespace>',
            '<classAnnotations>',
            '<className>',
            '<implements>',
            '<traits>'
        );

        $replacements = array(
            $this->namespace,
            $this->generateClassAnnotations($meta->getTraits()),
            $meta->getClassName(),
            $this->generateImplements($meta->getInterfaces()),
            $this->generateTraitStatements($meta->getTraits())
        );

        $code = str_replace($placeholders, $replacements, self::$template);
        return str_replace('<spaces>', self::$spaces, $code);
    }
29525
protected function processMetadata(MetadatableInterface $entity, EntityManager $entityManager)
    {
        if (null === $entity->getMetadatas()) {
            return;
        }

        foreach ($entity->getMetadatas() as $metadata) {
            if ($metadata instanceof Metadata) {
                $metadata
                    ->setHash($this->getMetadatableManager()->generateHash($entity))
                    ->setObjectClassName($this->getMetadatableManager()->getObjectClassName($entity))
                    ->setObjectId($this->getMetadatableManager()->getObjectId($entity))
                    ->setObject($entity)
                ;
                $entityManager->persist($metadata);
            }
        }
    }
500875
private function getFieldsFromMetadata(ClassMetadataInfo $metadata)
    {
        $fields = (array)$metadata->fieldNames;
//        var_dump($fields);


        // Remove the primary key field if it's not managed manually
        if (!$metadata->isIdentifierNatural()) {
            $fields = array_diff($fields, $metadata->identifier);
        }

//        foreach ($metadata->associationMappings as $fieldName => $relation) {
//            if ($relation['type'] !== ClassMetadataInfo::ONE_TO_MANY) {
//                $fields[$fieldName] = $fieldName;
//            }
//        }
//        dump($fields);
//        exit;
//
//        dump($metadata);


        foreach ($fields as $field) {
            if (array_key_exists($field, $metadata->fieldMappings)) {
                $fields[$field] = $this->map[$metadata->fieldMappings[$field]['type']];
            }

        }
//        dump($fields);
//        exit;
        return $fields;
    }
601850
protected function getInstance(ClassMetadata $metadata)
    {
        $reflection = new ReflectionClass($metadata->getName());
        $instance   = $reflection->newInstanceWithoutConstructor();

        return $instance;
    }
1117085
private function getEntityName($entityClass)
    {
        if ($this->metadataManager->hasMetadata($entityClass)) {
            return $this->metadataManager->getMetadata($entityClass)->getEntityName();
        }
        if ($this->translatableManager->isTranslation($entityClass)) {
            /** @var \Knp\DoctrineBehaviors\Model\Translatable\Translation $entityClass */
            $translatableClass = $entityClass::getTranslatableEntityClass();

            if ($this->metadataManager->hasMetadata($translatableClass)) {
                return $this->metadataManager->getMetadata($translatableClass)->getEntityName().'_translation';
            }
        }

        return $this->objectNamer->name($entityClass);
    }
776790
private function buildBodyRowAttr($entity, Metadata $meta)
    {
        $attr = [];

        foreach ($meta->getConfiguration()['index_view_row_attr'] as $property) {
            $attr['data-'.$property] = $this->propertyAccessor->getValue($entity, $property);
        }

        return $attr;
    }
227159
public function create($params = [], ClassMetadata $metadata = null)
    {
        $params = new ArrayObject($params);
        $builder = $this->getEntityBuilder();
        /** @var Entity $entity */
        $entity = $builder->getEntity();
        $preEventArgs = new HydrationEvent($params, ['entity' => $entity, 'repository' => $this]);
        $this->event()->dispatch(StorageEvents::PRE_HYDRATE, $preEventArgs);
        $builder->create($params, $entity);
        $postEventArgs = new HydrationEvent($params, ['entity' => $entity, 'repository' => $this]);
        $this->event()->dispatch(StorageEvents::POST_HYDRATE, $postEventArgs);

        return $entity;
    }
134192
private function generateMeta(bbMetaData $meta)
    {
        if (0 === $meta->count() || 'title' === $meta->getName()) {
            return '';
        }

        $result = '<meta ';
        foreach ($meta as $attribute => $value) {
            if (false !== strpos($meta->getName(), 'keyword') && 'content' === $attribute) {
                $keywords = explode(',', $value);
                foreach ($this->getKeywordObjects($keywords) as $object) {
                    $value = trim(str_replace($object->getUid(), $object->getKeyWord(), $value), ',');
                }
            }

            if ('content' === $attribute && empty($value)) {
                $result = '';
                break;
            }

            $result .= $attribute . '="' . html_entity_decode($value, ENT_COMPAT, 'UTF-8') . '" ';
        }

        return empty($result) ? $result : $result.'/>'.PHP_EOL;
    }
1324682
private function extractIdAndType(EntityMetadata $metadata, array &$data)
    {
        $identifier = $this->extractField(Persister::IDENTIFIER_KEY, $data);
        unset($data[Persister::IDENTIFIER_KEY]);

        $key = Persister::POLYMORPHIC_KEY;
        $type = $this->extractType($metadata, $data);
        if (isset($data[$key])) {
            unset($data[$key]);
        }
        return [$identifier, $type];
    }
129397
protected function generateMorphableClasses(array &$metadata)
    {
        foreach ($metadata as $key => $entityMetadata) {
            foreach ($entityMetadata['relations'] as $relationKey => $relationMetadata) {
                // get morphable classes for morphTo relations
                if ($relationMetadata['type'] == 'morphTo') {
                    $metadata[$key]['relations'][$relationKey]['options']['morphableClasses']
                        = $this->getMorphableClasses($entityMetadata['class'], $relationMetadata['options']['morphName'], $metadata);
                }

                // get morphable classes for morphToMany relations
                if ($relationMetadata['type'] == 'morphToMany' && ! $relationMetadata['options']['inverse']) {
                    $metadata[$key]['relations'][$relationKey]['options']['morphableClasses']
                        = $this->getMorphableClasses($entityMetadata['class'], $relationMetadata['options']['morphName'], $metadata, true);
                }
            }
        }
    }
1003073
private function buildFieldMappings(ClassMetadataInfo $metadata)
    {
        $tableName      = $metadata->table['name'];
        $columns        = $this->tables[$tableName]->getColumns();
        $primaryKeys    = $this->getTablePrimaryKeys($this->tables[$tableName]);
        $foreignKeys    = $this->getTableForeignKeys($this->tables[$tableName]);
        $allForeignKeys = array();

        foreach ($foreignKeys as $foreignKey) {
            $allForeignKeys = array_merge($allForeignKeys, $foreignKey->getLocalColumns());
        }

        $ids           = array();
        $fieldMappings = array();

        foreach ($columns as $column) {
            if (in_array($column->getName(), $allForeignKeys)) {
                continue;
            }

            $fieldMapping = $this->buildFieldMapping($tableName, $column);

            if ($primaryKeys && in_array($column->getName(), $primaryKeys)) {
                $fieldMapping['id'] = true;
                $ids[] = $fieldMapping;
            }

            $fieldMappings[] = $fieldMapping;
        }

        // We need to check for the columns here, because we might have associations as id as well.
        if ($ids && count($primaryKeys) == 1) {
            $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
        }

        foreach ($fieldMappings as $fieldMapping) {
            $metadata->mapField($fieldMapping);
        }
    }
860115
public function addMetadata(Metadata $metadata)
    {
        if (isset($this->metadata[$metadata->getEntityClass()])) {
            throw new MetadataException(sprintf('Metadata for entity "%s" is already added.', $metadata->getEntityClass()));
        }

        $this->metadata[$metadata->getEntityClass()] = $metadata;
    }
64763
private function getProperties(ClassMetadata $metadata)
    {
        return array_merge(
            $metadata->getFieldNames(),
            array_map(function (array $mapping) {
                return $mapping['fieldName'];
            }, $metadata->getEmbeddedFieldsMappings())
        );
    }
1237753
public function getTableName(ClassMetadataInfo $metadata)
    {
        $tableName = $metadata->getTableName();

        //## Fix for doctrine/orm >= 2.5
        if (method_exists($metadata, 'getSchemaName') && $metadata->getSchemaName()) {
            $tableName = $metadata->getSchemaName() . '.' . $tableName;
        }

        return $this->getTablePrefix() . $tableName . $this->getTableSuffix();
    }
654040
public function loadMetadataForClass($className, ClassMetadata $metadata)
    {
        foreach ($this->getJoinEntityConfigCollection() as $targetClassName => $config) {
            if ($this->getAuditObjectManager()
                ->getRepository(Entity\AuditEntity::class)
                ->generateClassName($targetClassName) == $className
            ) {
                $foundClassName = true;
                break;
            }
        }

        // @codeCoverageIgnoreStart
        if (! $foundClassName) {
            return false;
        }
        // @codeCoverageIgnoreEnd

        $metadataFactory = $this->getObjectManager()->getMetadataFactory();
        $builder = new ClassMetadataBuilder($metadata);
        $metadata = $metadataFactory->getMetadataFor($config['ownerEntity']);

        foreach ($metadata->getAssociationMappings() as $mapping) {
            if (isset($mapping['joinTable'])) {
                if ($mapping['joinTable']['name'] == $config['tableName']) {
                    $foundJoinEntity = true;
                    break;
                }
            }
        }

        // @codeCoverageIgnoreStart
        if (! $foundJoinEntity) {
            throw new Exception(
                'joinTable '
                . $targetClassName
                . ' not found by tableName '
                . $config['tableName']
                . ' on ownerEntity: '
                . $config['ownerEntity']
            );
        }
        // @codeCoverageIgnoreEnd

        $fields = [];
        foreach ($mapping['joinTable']['joinColumns'] as $column) {
            $column['dataType'] = $this->getObjectManager()
                ->getClassMetadata($mapping['sourceEntity'])
                ->getTypeOfField($column['referencedColumnName']);
            $fields[] = $column;
        }
        foreach ($mapping['joinTable']['inverseJoinColumns'] as $column) {
            $column['dataType'] = $this->getObjectManager()
                ->getClassMetadata($mapping['targetEntity'])
                ->getTypeOfField($column['referencedColumnName']);
            $fields[] = $column;
        }

        $identifiers = [];
        foreach ($fields as $field) {
            $builder->addField(
                $field['name'],
                $field['dataType'],
                [
                // 'nullable' => $config['nullable'],
                    'columnName' => $field['name'],
                    'id' => true,
                ]
            );
        }

        $association = $builder->createManyToOne('revisionEntity', Entity\RevisionEntity::class);
        $association->makePrimaryKey();
        $association->build();

        $builder->setTable(
            $this->getAuditOptions()->getAuditTableNamePrefix()
            . $config['tableName']
            . $this->getAuditOptions()->getAuditTableNameSuffix()
        );
    }
994890
private function validateCollectionNaming(EntityMetadata $metadata)
    {
        $persistence = $metadata->persistence;
        if (false === $this->entityUtil->isEntityTypeValid($persistence->collection)) {
            throw MetadataException::invalidMetadata(
                $metadata->type,
                sprintf('The entity persistence collection "%s" is invalid based on the configured name format "%s"',
                        $persistence->collection,
                        $this->entityUtil->getRestConfig()->getEntityFormat()
                )
            );
        }
    }
469994
private function mapTranslatable(ClassMetadata $metadata)
    {
        // In the case A -> B -> TranslatableInterface, B might not have mapping defined
        // as it is probably defined in A, so in that case, we just return.
        if (!isset($this->configs[$metadata->name])) {
            return;
        }

        $metadata->mapOneToMany([
            'fieldName'     => 'translations',
            'targetEntity'  => $this->configs[$metadata->name],
            'mappedBy'      => 'translatable',
            //'fetch'         => ClassMetadataInfo::FETCH_EXTRA_LAZY,
            'indexBy'       => 'locale',
            'cascade'       => ['persist', 'merge', 'refresh', 'remove', 'detach'],
            'orphanRemoval' => true,
        ]);

        $metadata->addEntityListener('preFlush', self::class, 'onTranslatablePreFlush');
    }