Filter tables to be mapped by Doctrine from Entitymanager (no command line)

Asked

Viewed 443 times

1

I’m developing a feature in my application that consists of automatically mapping and generating entities.

The problem is that the user can select the tables he wants to map, so I must insert a filter with the name of the tables to be converted. To do this in the command line just insert the --filter parameter as the example below:

php doctrine orm:convert-mapping --from-database --force --filter='class_name' xml /path_to_app/entities/metadata

But I am developing this functionality in my application to get the return of a possible error, using the Doctrine library esctrita in PHP. That is why I am creating the function below that performs the mapping of all tables to xml (I prefer this format), and later creates the entities from the mapping. However, I don’t know how to filter the name of the tables I want to map, i.e., insert the functionality performed by the parameter --filter='class_name'.

public function mapearTabelasValidar() {

        //Inicializa variáveis
        $dirBin = DIR_THIRD_PARTY . 'composer/vendor/bin/';
        $dirEntidades = DIR_MODELS . 'entidades/';
        $dirMetadados = $dirEntidades . 'metadados/';
        $title = 'Modelos gerados com sucesso!';

        try {

            //Inicializa o Doctrine e a configuração do EntityManager
            $doctrine = new Doctrine();
            $em = $doctrine->em;
            $em->getConfiguration()->setMetadataDriverImpl(
                    new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
                    $em->getConnection()->getSchemaManager()
                    )
            );

            //Define os metadados 
            $cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
            $cmf->setEntityManager($em);
            $metadata = $cmf;

            /**
             * Exporta os metadados das entidades em format xml
             * 
             * Comando similar:
             * php doctrine orm:convert-mapping --from-database --force xml /var/www/fwsibe/sistema/models/entidades/metadados
             */
            //echo "<pre>";
            //print_r($metadata);
            //die();
            $cme = new \Doctrine\ORM\Tools\Export\ClassMetadataExporter();
            $exporter = $cme->getExporter('xml', $dirMetadados);
            $exporter->setMetadata($metadata->getAllMetadata());
            $exporter->export();

            /**
             * Gera as entidades a partir do mapeamento XML gerado anteriormente
             * 
             * Comando similar:
             * php doctrine orm:generate:entities --generate-methods=1 --update-entities=1 /var/www/fwsibe/sistema/models/entidades
             */
            $generator = new \Doctrine\ORM\Tools\EntityGenerator();
            $generator->setGenerateStubMethods(true);
            $generator->setRegenerateEntityIfExists(false);
            $generator->generate($metadata->getAllMetadata(), $dirEntidades);

            //Trata a possível exeção
        } catch (Exception $exc) {
            mensagemExcecao($exc, "Falha ao mapear tabelas!", true);
        }
    }

Would anyone have any suggestions?

1 answer

1

you can try using the metadatafilter class in accordance with the command itself available in the Doctrine project use, in your code would look something like this:

...
$filter = 'string de entidades filtradas';
$metadata = MetadataFilter::filter($metadata, $filter);
$exporter->setMetadata($metadata);
...

do not forget to import the class using the directive use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.