mayconfsbrito mayconfsbrito - 6 months ago 31
PHP Question

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

I'm developing a funcionality on my application that consists on mapping and generate entities automatically.

The problem is that the user can calect the tables to map, that is why I must to insert a filter for the name of the tables to be converted. To do this in command line, I just need to insert the parameter --filter:

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


However I'm developing the funcionality below in PHP, in order to obtain the return of a possible error. This function maps all the tables on database to XML, and posteriorly creates the entities from the mapping. In the meantime I don't know how to filter the tables by the name through the Doctrine PHP Library, in other words, how to do 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);
}
}


Thanks!

Answer

With Doctrine 2.4, when you make a call to doctrine orm:convert-mapping --from-database --filter note that the filter is done with the function strpos from PHP. The filter acts like it's searching for "table%", so will find any entity beginning with that string filtered.

You can override or change the method that build the search for a better filter. This describes how do that: doctrine mapping

Doctrine 2.5's MetadataFilter::accept() was updated to use a preg_match() rather than strpos()