Zend Framework 2 Doctrine 2 CLI生成—来自YAML的每个模块的实体

时间:2021-11-17 06:48:01

I have clean project ZendSkeletonApplication with integrated Doctrine 2 module "doctrine-orm-module" etc via Composer. Doctrine CLI works from vendor/bin.

我有一个干净的项目zendskeleton的应用程序与集成的学说2模块“学说-真模”通过编写器等。教条CLI从供应商/bin工作。

I have 'Application' and 'Blog' module, my module config:

我有“应用”和“博客”模块,我的模块配置:

<?php
namespace Blog;

return array(
  'router' => array(
    'routes' => array(
      'post' => array(
        'type' => 'segment',
        'options' => array(
          'route' => '/post[/:action][/:id]',
          'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id' => '[0-9]+',
          ),
          'defaults' => array(
            'controller' => 'Blog\Controller\Post',
            'action' => 'index',
          ),
        ),
      ),
    ),
  ),
  'controllers' => array(
    'invokables' => array(
      'Blog\Controller\Post' => 'Blog\Controller\PostController'
    ),
  ),
  'view_manager' => array(
    'template_path_stack' => array(
      'blog' => __DIR__ . '/../view',
    ),
  ),
  'doctrine' => array(
    'driver' => array(
      __NAMESPACE__ . '_driver' => array(
        'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
        'cache' => 'array',
        'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
      ),
      'orm_default' => array(
        'drivers' => array(
          __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
        )
      )
    )
    )
);

How to generate Entities from YAML files each module and how to config my modules arrays to use YAML? For example I have my all .yml files in ZendSkeletonApplication/mapping/yml and few .yml files have definitions of Blog module entities and few have definitions of Application module entities.

如何从YAML文件中生成实体,如何配置模块数组来使用YAML?例如,我在zendskeleton onapplication /mapping/yml中有我的所有.yml文件,很少有.yml文件定义了博客模块实体,很少有文件定义了应用模块实体。

My entities are in Blog/src/Blog/Entity folder for blog module. All I want its just by one call in Doctrine CLI generate-entities create all Entities each module from all .yml files which are placed in mapping/yml folder? Is it possible? Can anybody provide simple example with doctrine config?

我的实体在Blog/src/Blog/Entity文件夹中,用于Blog模块。所有我想要的仅仅是一个调用,在Doctrine CLI中,实体创建所有的实体每个模块从所有的。yml文件中被放置在映射/yml文件夹中?是可能的吗?有人能提供一个简单的例子说明配置吗?

4 个解决方案

#1


2  

The following approach quick and dirty approach works for me:

下面的方法快速而肮脏的方法对我有效:

  1. Add the following lines to ...vendor\doctrine\doctrine-module\bin\doctrine-module.php:

    将以下几行添加到…供应商原则\原则-模块\程序-模块\程序-模块。php:

    $driverImpl = new \Doctrine\ORM\Mapping\Driver\YamlDriver(
         array("YOUR_PATH_TO_YAML_FILES"));
    /* @var $em \Doctrine\ORM\EntityManager */
    $em = $application->getServiceManager()->get('doctrine.entitymanager.orm_default');
    $em->getConfiguration()->setMetadataDriverImpl($driverImpl);
    
    //...old code...
    /* @var $cli \Symfony\Component\Console\Application */        
    $cli = $application->getServiceManager()->get('doctrine.cli');
    
  2. Now you can this doctrine-module.php on command line interface to call

    现在你可以用这个理论模块了。php命令行接口调用

     orm:generate-entities --generate-annotations=1 PATH_TO_YOUR_ENTITY_CLASSES
    

    Be careful with namespaces. The YAML driver expects namespace.entity.dcm.yml to be the the name of the \Namespace\Entity YAML file. The Tool will create PATH_TO_YOUR_ENTITY_CLASSES\Namespace\Entity.php for you.

    小心使用名称空间。YAML驱动程序期望namespace.entity.dcm。yml是\命名空间\实体YAML文件的名称。该工具将创建PATH_TO_YOUR_ENTITY_CLASSES\名称空间\实体。php。

If you want to use this approach more regularly it might be cleaner to extend Doctrine\ORM\Tools\Console\CommandGenerateEntitiesCommand along these lines and add a new command to the cli.

如果您想更经常地使用这种方法,那么扩展Doctrine\ORM\Tools\Console CommandGenerateEntitiesCommand沿着这些行并向cli添加一个新命令可能会更清晰。

#2


2  

Configure your doctrine driver this way on the module.config.php

在modu .config.php上以这种方式配置学说驱动程序

'doctrine' => array(
    'driver' => array(
        'orm_default' => array(
            'drivers' => array(
                'Application\Entity' => 'application_entities_yaml'
                //replace Application by your module namespace
            ),
        ),
        'application_entities_yaml' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
            'paths' => array(__DIR__ . '/../src/' .__NAMESPACE__.  '/Yml/')
            //should be where are yours Yml files.
        ),
    ),
),

#3


1  

To solve this problem make sure that you have generated your entities specifying a namespace for them. You command line for that should be something like this:

要解决这个问题,请确保您已经生成了为它们指定名称空间的实体。你的命令行应该是这样的:

vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping annotation module/MyNamespace/src/ --namespace="MyNamespace\Entity\\" --from-database

Whithout the option --namespace, your entities will not be inside a namespace, so the doctrine cannot find your entities.

在选项命名空间中,您的实体将不在命名空间中,因此该原则无法找到您的实体。

From here you can make use of annother doctrine commands like orm:generate-repositories (You need to configure your entities specifying the repository names) as follow:

从这里开始,您可以使用orm:generate- repository之类的注释教义命令(您需要配置指定存储库名称的实体),如下所示:

<?php

namespace MyNamespace\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="MyNamespace\Repository\UserRepository")
 */
class User
{
.....
}

#4


0  

DoctrineModule does not officially support entity generation. Use the native CLI of the ORM (./vendor/bin/doctrine) to generate your entities, then move them to the required location in your module.

教条主义并不支持实体生成。使用ORM (./vendor/bin/doctrine)的本机CLI生成实体,然后将它们移动到模块中所需的位置。

#1


2  

The following approach quick and dirty approach works for me:

下面的方法快速而肮脏的方法对我有效:

  1. Add the following lines to ...vendor\doctrine\doctrine-module\bin\doctrine-module.php:

    将以下几行添加到…供应商原则\原则-模块\程序-模块\程序-模块。php:

    $driverImpl = new \Doctrine\ORM\Mapping\Driver\YamlDriver(
         array("YOUR_PATH_TO_YAML_FILES"));
    /* @var $em \Doctrine\ORM\EntityManager */
    $em = $application->getServiceManager()->get('doctrine.entitymanager.orm_default');
    $em->getConfiguration()->setMetadataDriverImpl($driverImpl);
    
    //...old code...
    /* @var $cli \Symfony\Component\Console\Application */        
    $cli = $application->getServiceManager()->get('doctrine.cli');
    
  2. Now you can this doctrine-module.php on command line interface to call

    现在你可以用这个理论模块了。php命令行接口调用

     orm:generate-entities --generate-annotations=1 PATH_TO_YOUR_ENTITY_CLASSES
    

    Be careful with namespaces. The YAML driver expects namespace.entity.dcm.yml to be the the name of the \Namespace\Entity YAML file. The Tool will create PATH_TO_YOUR_ENTITY_CLASSES\Namespace\Entity.php for you.

    小心使用名称空间。YAML驱动程序期望namespace.entity.dcm。yml是\命名空间\实体YAML文件的名称。该工具将创建PATH_TO_YOUR_ENTITY_CLASSES\名称空间\实体。php。

If you want to use this approach more regularly it might be cleaner to extend Doctrine\ORM\Tools\Console\CommandGenerateEntitiesCommand along these lines and add a new command to the cli.

如果您想更经常地使用这种方法,那么扩展Doctrine\ORM\Tools\Console CommandGenerateEntitiesCommand沿着这些行并向cli添加一个新命令可能会更清晰。

#2


2  

Configure your doctrine driver this way on the module.config.php

在modu .config.php上以这种方式配置学说驱动程序

'doctrine' => array(
    'driver' => array(
        'orm_default' => array(
            'drivers' => array(
                'Application\Entity' => 'application_entities_yaml'
                //replace Application by your module namespace
            ),
        ),
        'application_entities_yaml' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
            'paths' => array(__DIR__ . '/../src/' .__NAMESPACE__.  '/Yml/')
            //should be where are yours Yml files.
        ),
    ),
),

#3


1  

To solve this problem make sure that you have generated your entities specifying a namespace for them. You command line for that should be something like this:

要解决这个问题,请确保您已经生成了为它们指定名称空间的实体。你的命令行应该是这样的:

vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping annotation module/MyNamespace/src/ --namespace="MyNamespace\Entity\\" --from-database

Whithout the option --namespace, your entities will not be inside a namespace, so the doctrine cannot find your entities.

在选项命名空间中,您的实体将不在命名空间中,因此该原则无法找到您的实体。

From here you can make use of annother doctrine commands like orm:generate-repositories (You need to configure your entities specifying the repository names) as follow:

从这里开始,您可以使用orm:generate- repository之类的注释教义命令(您需要配置指定存储库名称的实体),如下所示:

<?php

namespace MyNamespace\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="MyNamespace\Repository\UserRepository")
 */
class User
{
.....
}

#4


0  

DoctrineModule does not officially support entity generation. Use the native CLI of the ORM (./vendor/bin/doctrine) to generate your entities, then move them to the required location in your module.

教条主义并不支持实体生成。使用ORM (./vendor/bin/doctrine)的本机CLI生成实体,然后将它们移动到模块中所需的位置。