Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 Doctrine 2 CLI generate-entities from YAML each module

I have clean project ZendSkeletonApplication with integrated Doctrine 2 module "doctrine-orm-module" etc via Composer. Doctrine CLI works from vendor/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.

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?

like image 496
JokerDark2 Avatar asked Jan 25 '13 15:01

JokerDark2


3 Answers

The following approach quick and dirty approach works for me:

  1. Add the following lines to ...vendor\doctrine\doctrine-module\bin\doctrine-module.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

     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.

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.

like image 53
ACNB Avatar answered Sep 28 '22 00:09

ACNB


Configure your doctrine driver this way on the module.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.
        ),
    ),
),
like image 36
Mauricio Piber Fão Avatar answered Sep 28 '22 00:09

Mauricio Piber Fão


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:

<?php

namespace MyNamespace\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="MyNamespace\Repository\UserRepository")
 */
class User
{
.....
}
like image 42
Tales Santos Avatar answered Sep 28 '22 00:09

Tales Santos