使用doctrine生成器在symfony上创建我自己的实体生成器

时间:2014-05-23 09:07:17

标签: symfony doctrine entity generator

我正在构建一个实体生成器,以便根据远程数据库(DB)通过SOAP WS更新我的数据库结构和数据......

我从数组中获取结构,目前正在编写我的实体类,并使用file_put_content()。

但随着我的进步,我无法注意到Doctrine \ ORM \ Tools \ EntityGenerator已经做了我打算做的事情,我会做得更好!!

所以我想知道是否有办法从我的控制器中使用这个实现的生成器? 我的意思是,这就是Symfony的意义所在吗?使用现有的课程和从我的代码中的任何地方捆绑? :)

有什么想法吗?

Thx:)

1 个答案:

答案 0 :(得分:3)

您可以使用此代码:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator;
use Sensio\Bundle\GeneratorBundle\Command\Validators;

class MyController extends Controller
{
    private $generator;

    public function generateEntityAction()
    {
        $format = "annotation"; //it can also be yml/php/xml
        $fields = "title:string(255) body:text";
        $withRepository = false; //true/false

        $entity = Validators::validateEntityName("MyBundle:EntityName");
        list($bundle, $entity) = $this->parseShortcutNotation($entity);
        $format = Validators::validateFormat($format);
        $fields = $this->parseFields($fields);
        $bundle = $this->get('service_container')->get('kernel')->getBundle($bundle);
        $generator = $this->getGenerator();
        $generator->generate($bundle, $entity, $format, array_values($fields), $withRepository);
    }

    protected function createGenerator()
    {
        return new DoctrineEntityGenerator($this->get('service_container')->get('filesystem'), $this->get('service_container')->get('doctrine'));
    }

    protected function getSkeletonDirs(BundleInterface $bundle = null)
    {
         $skeletonDirs = array();

         if (isset($bundle) && is_dir($dir = $bundle->getPath() . '/Resources/SensioGeneratorBundle/skeleton')) {
         $skeletonDirs[] = $dir;
         }

         if (is_dir($dir = $this->get('service_container')->get('kernel')->getRootdir() . '/Resources/SensioGeneratorBundle/skeleton')) {
         $skeletonDirs[] = $dir;
         }

         $skeletonDirs[] = __DIR__ . '/../Resources/skeleton';
         $skeletonDirs[] = __DIR__ . '/../Resources';

         return $skeletonDirs;
     }

     protected function getGenerator(BundleInterface $bundle = null)
     {
         if (null === $this->generator) {
              $this->generator = $this->createGenerator();
              $this->generator->setSkeletonDirs($this->getSkeletonDirs($bundle));
         }

         return $this->generator;
     }

     protected function parseShortcutNotation($shortcut)
     {
         $entity = str_replace('/', '\\', $shortcut);

         if (false === $pos = strpos($entity, ':')) {
             throw new \InvalidArgumentException(sprintf('The entity name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Blog/Post)', $entity));
         }

         return array(substr($entity, 0, $pos), substr($entity, $pos + 1));
     }

     private function parseFields($input)
     {
         if (is_array($input)) {
             return $input;
         }

         $fields = array();
         foreach (explode(' ', $input) as $value) {
             $elements = explode(':', $value);
             $name = $elements[0];
             if (strlen($name)) {
                 $type = isset($elements[1]) ? $elements[1] : 'string';
                 preg_match_all('/(.*)\((.*)\)/', $type, $matches);
                 $type = isset($matches[1][0]) ? $matches[1][0] : $type;
                 $length = isset($matches[2][0]) ? $matches[2][0] : null;

                 $fields[$name] = array('fieldName' => $name, 'type' => $type, 'length' => $length);
             }
         }

         return $fields;
     }
}

大多数情况下,它来自Sensio \ Bundle \ GeneratorBundle \ Command \ GenerateDoctrineEntityCommand和Sensio \ Bundle \ GeneratorBundle \ Command \ GeneratorCommand