mayconfsbrito mayconfsbrito - 1 year ago 164
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;
new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(

//Define os metadados
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
$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>";
$cme = new \Doctrine\ORM\Tools\Export\ClassMetadataExporter();
$exporter = $cme->getExporter('xml', $dirMetadados);

* 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->generate($metadata->getAllMetadata(), $dirEntidades);

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


Answer Source

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()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download