Filter tables to be mapped by Doctrine from the EntityManager (without command line)

0

I'm developing a feature in my application that automatically map and generate entities.

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

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

But I'm developing this functionality in my application to get the return of a possible error, using the library of doctrine esctrita in PHP. So 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 do not know how to filter the name of the tables I want to map, that is, insert the functionality made by the - filter = 'class_name' parameter.

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?

    
asked by anonymous 17.07.2014 / 13:46

1 answer

1

You can try using the a> as the command available in the Doctrine project uses, in its 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 use directive.

    
21.10.2014 / 15:45