Перейти к основному содержанию

Doctrine cli-config.php и запуск doctrine generate

Первый вариант

README.txt

To generate your database-based entities using doctrine, follow these steps:

**Use composer to set up doctrine in your project**

1 - Copy and paste the files cli-config.php and doctrine-bootstrap.php into a directory of your application. (Recommended: Create a directory in the root called bin)
2 - In the doctrine-bootstrap.php file, configure your database access credentials
3 - To generate the entities, first we will generate the metadata of what will be the entity. To do this, execute the following command:
3.1 - /vendor/bin/doctrine orm:convert-mapping xml ../metadata --from-database --force

*In the above command, you specified the doc's bin address and where the files should be generated, correct them according to your project.*

4 - Now, just ask for the doctrine to generate the entities based on the metadata. Do this with the following command:
4.1 - 

cli-config.php

<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
require_once 'doctrine-bootstrap.php';
return ConsoleRunner::createHelperSet($entityManager);

doctrine-bootstrap.php

<?php
define('ROOT_PATH',realpath(dirname(__DIR__)).DIRECTORY_SEPARATOR);

require(ROOT_PATH.'vendor/autoload.php');

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array(ROOT_PATH."metadata/");
$isDevMode = true;

$dbParams = array(
    'driver'    => 'pdo_mysql',
    'user'      => 'userName',
    'password'  => 'userPass',
    'dbname'    => 'userDbScheme',
    'charset'   => 'utf8',
    'host'  =>  'dbHost'
);

$config = Setup::createXMLMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

#Fix type missing
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
$platform->registerDoctrineTypeMapping('set', 'string');

Второй вариант

bootstrap.php

<?php

require_once "vendor/autoload.php";

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array("./src/","./metadata/");
$isDevMode = true;

// the connection configuration
$dbParams = array(
    'driver'    => 'pdo_mysql',
    'user'      => 'root',
    'password'  => '123456789',
    'dbname'    => 'test',
    'charset'   => 'utf8'
);

$config = Setup::createXMLMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

#Fix type missing
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
$platform->registerDoctrineTypeMapping('set', 'string');


#Test Final 
# Inicia o processo com $config = Setup::createXMLMetadataConfiguration($paths, $isDevMode);
# run php vendor/bin/doctrine orm:convert-mapping --force --from-database --namespace="Namespace\\Model\\" xml ./metadata/
# adicione o atributo repository-class ao final da tag <entity> com o namespace do EntityRepository responsável por aquela entidade
# run php vendor/bin/doctrine orm:generate-entities --generate-annotations=1 --generate-methods=1 ./src/
# run php vendor/bin/doctrine orm:generate:repositories ./src/
# deleta os arquivos metadata *.xml
# substitui todos os inicios de comentarios com @ORM\ por => @
# Apenas nas classes de modelo geradas

cli-config.php

<?php
# path config/cli-config.php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'bootstrap.php';
return ConsoleRunner::createHelperSet($entityManager);

Третий вариант

bootstrap.php

<?php

// bootstrap.php
// Include Composer Autoload (relative to project root).
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

date_default_timezone_set('America/Lima');

require_once "vendor/autoload.php";

// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
//$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/src"), $isDevMode);
// or if you prefer yaml or XML
//$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
$config = Setup::createYAMLMetadataConfiguration(array(__DIR__ . "/config/yaml"), $isDevMode);
// the connection configuration
$conn = array(
  'driver' => 'pdo_mysql',
  'user' => 'root',
  'password' => 'password',
  'dbname' => 'dbname',
  'port' => '3306'
);

// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);

cli-config.php

<?php

use Doctrine\ORM\Tools\Console\ConsoleRunner;

// Replace it to your own project bootstrap.php file
require_once 'bootstrap.php';

return ConsoleRunner::createHelperSet($entityManager);

composer.json

{
  "require": {
    "doctrine/orm": "*",
    "symfony/yaml": "*"
  }
}  

doctrine-genearator.sh

#!/bin/sh

# Doctrine 2 Entities Generator v0.1
# ==================================

# Requirements
# ============
# It's necessary to create a composer project before exec this script.
# More info about composer at https://getcomposer.org/doc/00-intro.md


echo Doctrine 2 Entities Generator v0.1
echo ==================================

# Remove ./config/yaml directory before
rm -rf ./config/yaml

# Create yaml directory
mkdir ./config/yaml

# Read the ./cli-config.php (by default) and generate mapping yaml files to ./config/yaml directory
php vendor/bin/doctrine orm:convert-mapping --namespace="" --force --from-database yml ./config/yaml

# Generated models to ./src directory
php vendor/bin/doctrine orm:generate-entities --generate-annotations=false --update-entities=true --generate-methods=false ./src

# Validate schema
php vendor/bin/doctrine orm:validate-schema