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?