duanfei1268 2014-07-17 12:09
浏览 30
已采纳

从EntityManager过滤要由Doctrine映射的表(不带命令行)

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!

  • 写回答

1条回答 默认 最新

  • doulu4534 2014-07-18 14:19
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?