【问题标题】:Custom Symfony generation scripts for entities实体的自定义 Symfony 生成脚本
【发布时间】:2017-02-26 03:59:58
【问题描述】:

我只是想知道是否有任何方法可以修改实体生成脚本(使用 generate:doctrine:entity 调用)。

例如脚本像这样创建EntityRepository

use Doctrine\ORM\EntityRepository;

/**
 * FreeTokenRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class FreeTokenRepository extends EntityRepository
{
}

但我想让 EntityRepository 像这样扩展我自己的 Repository 子类(并且还对括号使用不同的约定)

use AppBundle\Model\Repository;

/**
 * FreeTokenRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class FreeTokenRepository extends Repository {
}

有没有办法自定义 symfony/doctrine 实体创建脚本?和所有其他生成脚本?所以我不必每次都更改自动生成的类?

【问题讨论】:

    标签: php symfony doctrine entity auto-generate


    【解决方案1】:

    我们为此创建了自己的命令。我将在这里与您分享代码,因为它似乎完全符合您的要求。

    当然,您必须调整一些文件夹等,但基本上它完全符合您的要求。 只需根据您的其他需求进行调整即可。

    只需调用 app (Symfony 2) 或 bin (Symfony 3) /console company:createrepository MyEntity Extends,它就会创建您的存储库文件。

    <?php
    
    namespace Company\MyBundle\Command;
    
    use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
    use Symfony\Component\Console\Input\InputArgument;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Component\Console\Question\ConfirmationQuestion;
    use Symfony\Component\HttpKernel\Kernel;
    
    /**
     * Creates a Repository File for a given Entity Class
     *
     * You will find a file called EntityRepository.php in Repositories folder
     * Reformat the output code and enjoy
    *
    *
    */
    class CreaterepositoryCommand extends ContainerAwareCommand
    {
    /**
     *
     */
    protected function configure()
    {
        $this
            ->setName('company:createrepository')
            ->setDescription('Create an Repository for entity class')
            ->addArgument('entity', InputArgument::REQUIRED, 'Enter an entity class name')
            ->addArgument(
                'extends',
                InputArgument::OPTIONAL,
                'Enter which other Repository should be extended');
    }
    
    /**
     * @param InputInterface  $input
     * @param OutputInterface $output
     *
     * @return int|null|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
    
        $manualEntity = $input->getArgument('entity');
    
        if ($input->getArgument('extends')) {
            $extends = $input->getArgument('extends') . "Repository";
            $use = '';
        } else {
            $extends = "EntityRepository";
            $use = '
            use Doctrine\ORM\EntityRepository;
            ';
        }
    
        $fileContent = '
        <?php
    
            namespace Company\MyBundle\Entity\Repositories;
            ' . $use . '
            class ' . $manualEntity . 'Repository extends ' . $extends . '
            {
    
            } ';
    
        /** @var Kernel $kernel */
        $kernel = $this->getContainer()->get('kernel');
        $path = $kernel->locateResource('@CompanyMyBundle');
        $path .= 'Entity/Repositories';
        $fileName = $manualEntity . "Repository.php";
        $fileNameInclPath = $path . "/" . $fileName;
    
        if (file_exists($fileNameInclPath)) {
            $helper = $this->getHelper('question');
            $question = new ConfirmationQuestion(
                '<fg=blue>File: </fg=blue>' . $fileName . '<fg=blue> already exists Overwrite?</fg=blue>',
                false);
    
            if (!$helper->ask($input, $output, $question)) {
                $output->writeln('<fg=red>Aborted by user.</fg=red>');
            } else {
                $fp = fopen($fileNameInclPath, "wb");
                fwrite($fp, $fileContent);
                fclose($fp);
                $output->writeln('<fg=green>File:</fg=green> ' . $fileName . '<fg=green> created</fg=green>');
            }
        } else {
            $fp = fopen($fileNameInclPath, "wb");
            fwrite($fp, $fileContent);
            fclose($fp);
            $output->writeln('<fg=green>File:</fg=green> ' . $fileName . '<fg=green> created</fg=green>');
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 2017-02-26
      相关资源
      最近更新 更多