Doctrine2 - Generate From Database - php

I successfully generated entities from an existing mysql database, but I fail when trying to generate repository classes.
My doctrine.config.php
$frapi_config = array(
'database' => array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'root',
'dbname' => 'PizzaVan',
'host' => 'localhost:3306'
),
'doctrine' => array (
// Base path is /src/frapi/
'entitiesFolder' => "custom/Application/Model"
)
);
My bootstrap.php
//bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once dirname(__FILE__) . "/../vendor/autoload.php";
require_once "doctrine.config.php";
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/../" . $frapi_config["doctrine"]["entitiesFolder"]), $isDevMode, null, null, false);
// database configuration parameters
$conn = $frapi_config["database"];
// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
My cli-config.php
require_once "bootstrap.php";
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
As said before, if I run 2 commands from the console
./vendor/bin/doctrine orm:convert-mapping --namespace='custom\\Application\\Model\\' --force --from-database annotation .
and
./vendor/bin/doctrine orm:generate-entities . --generate-annotations=true --generate-methods=true
I get Entities correctly created in the right folder. Now I would like to create custom RepositoryClass, but if I run
./vendor/bin/doctrine orm:generate-repositories .
I get this message : "No Repository classes were found to be processed".
Any solution? Thank you!

Related

Database created with doctrine, not shown in phpmyadmin

I am following the tutorial for Doctrine: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/getting-started.html
I changed the bootstrap file to include my database to:
<?php
// bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once "vendor/autoload.php";
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = false;
$paths = array(__DIR__."/src");
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
// or if you prefer yaml or XML
//$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
//$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);
// database configuration parameters
$conn = array(
'host' => '*********',
'port' => '3306',
'user' => '********',
'password' => '****',
'dbname' => 'bugs',
'charset' => 'UTF8',
'driver' => 'pdo_sqlite',
'path' => __DIR__ . '/db.sqlite',
);
// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
Then, I did the rest and create the tables, etc using the following commnad:
vendor/bin/doctrine orm:schema-tool:create
Later, I logged to my 'myphpadmin' and the database was not there.
Am I missing any step?
Thank you
That is because you are using the driver pdo_sqlite. It is creating an SQLite database and not a MySQL database. It should have created the file db.sqlite.
Change it to pdo_mysql to get the database to show up in phpmyadmin.

Doctrine Setting up the Commandline Tool. You are missing a "cli-config.php"

There is a section in Doctrine documentation about Setting up the Commandline Tool
You need to register your applications EntityManager to the console
tool to make use of the tasks by creating a cli-config.php
So in my bootstrap I've got:
// core/Cms.php
private function __construct() {
$loader = require __DIR__ . '/../../vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
$paths = array(ENTITY_DIR);
$isDevMode = false;
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => DB_SERVER,
'port' => DB_PORT,
'user' => DB_USER,
'password' => DB_PASSWORD,
'dbname' => DB_NAME,
'charset' => 'utf8',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
$config->setProxyDir(CLASS_DIR . '/Proxies');
$config->setProxyNamespace('Application\Classes\Proxies');
$config->setAutoGenerateProxyClasses(Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
$entityManager = EntityManager::create($dbParams, $config);
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
self::$entityManager = $entityManager;
}
and in same project directory core/cli-config.php:
// core/cli-config.php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
require(SYS_DIR . '/core/Cms.php'); // my bootstrap
Cms::initialize();
$entityManager = Cms::$entityManager;
return ConsoleRunner::createHelperSet($entityManager);
Then command php vendor/bin/doctrine --help display:
You are missing a "cli-config.php" or "config/cli-config.php" file in
your project, which is required to get the Doctrine Console working.
working with:
vendor/bin/doctrine --help
instead of:
php vendor/bin/doctrine --help

Doctrine cannot redeclare class Annotation

i've got my bootstrap
<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
// Log Filename
define ('LOG_FILENAME', 'clicktocall-'.date("Ymd").'log');
$loader = require "vendor/autoload.php";
// Doctrine parser
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array("./src/acme"), $isDevMode);
// Database configuration parameters
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'xxx',
'password' => 'yyy',
'dbname' => 'zzz',
);
// Obtaining the entity manager
$em = EntityManager::create($dbParams, $config);
// Setting Logger Monolog
$Logger = new \Monolog\Logger('CTLogger');
$Logger->pushHandler(new \Monolog\Handler\StreamHandler('./log/'.LOG_FILENAME, \Monolog\Logger::INFO));
When i'm trying to execute
vendor\bin\doctrine orm:schema-tool:update --force
I obtain this message
Fatal error: Cannot redeclare class Doctrine\ORM\Mapping\Annotation in E:\www\acme\vendor\doctri
ne\orm\lib\Doctrine\ORM\Mapping\Annotation.php on line 22
I'm just using Doctrine bundle.
Any idea?
Thanks a lot
[Edit]
I've found that the problem is caused by
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
I use this row to parse the annotations for the symfony\validator bundle, without this the validation doesn't work...

Command line Doctrine ORM with Silex: You are missing a "cli-config.php" or "config/cli-config.php" file in your project

I am trying to use Doctrine ORM with Silex, and finding it an altogether frustrating experience, due to the lack of consistent documentation.
When I run vendor/bin/doctrine at the console, I get the following output:
output:
You are missing a "cli-config.php" or "config/cli-config.php" file in your
project, which is required to get the Doctrine Console working. You can use the
following sample as a template:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
// replace with file to your own project bootstrap
require_once 'bootstrap.php';
// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();
return ConsoleRunner::createHelperSet($entityManager);
This is my composer.json file:
{
"require": {
"silex/silex": "2.0.*#dev",
"symfony/yaml": "2.6.7",
"doctrine/dbal": "~2.2",
"dflydev/doctrine-orm-service-provider": "2.0.*#dev",
"khepin/yaml-fixtures-bundle": "~0.8.1"
},
"config": {
"bin-dir": "bin"
}
}
This is the php code that registers the Doctrine service etc.
<?php
use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\ArrayCache;
use Silex\Provider\DoctrineServiceProvider;
use Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider;
$app->register(new DoctrineServiceProvider(), array(
'db.options' => array(// http://silex.sensiolabs.org/doc/providers/doctrine.html
'driver' => 'pdo_mysql',
'dbname' => 'foobar',
'host' => 'localhost',
'user' => 'root',
'password' => 'root',
'charset' => 'utf8'
)
));
$app->register(new DoctrineORMServiceProvider(),
array(
'db.orm.proxies_dir' => __DIR__.'/../cache/doctrine/proxy',
'db.orm.proxies_namespace' => 'DoctrineProxy',
'db.orm.cache' => !$app['debug'] &&extension_loaded('apc') ? new ApcCache() : new ArrayCache(),
'db.orm.auto_generate_proxies' => true,
'db.orm.entities' => array(array(
'type' => 'simple_yaml',
'path' => __DIR__.'/src/Resources/config/doctrine',
'namespace' => 'Foobar\Entity',
)),
));
This is my configuration file (bin/cli-config.php)
<?php
// retrieve EntityManager
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
$app = require_once __DIR__.'/../app/src/app.php';
$isDevMode = $app['debug'];
$paths = $app['db.orm.entities']['path'];
$config = Setup::createYAMLMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($app['db.options'], $config);
return ConsoleRunner::createHelperSet($entityManager);
What am I doing wrong?
You need to move bin/cli-config.php in config/cli-config.php.
Unfortunately I have not found documentation about it. I opened doctrine/dbal/bin/doctrine-dbal.php and checked how it worked.
Both cli-config.php and bootstrap.php should be placed in same folder. They are working for me if either I place them at root of my zend project or in root->config folder.
Then orm:convert-mapping command in terminal on mac runs successfully.
./vendor/bin/doctrine orm:convert-mapping --namespace="Quiz\Model\Entity" --force --from-database annotation ./module/Quiz/src/Model/

Doctrine Console program

I am learning Doctrine. I config doctrine 2.2.0 by Tarball Download. Now getting trouble when generating-the-database-schema. Can't use command-line tool with the code below:
<?php
// doctrine.php - Put in your application root
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\DBAL\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Symfony\Component\Console\Helper\HelperSet;
$lib = "../DoctrineORM-2.2.0";
require $lib . '/Doctrine/ORM/Tools/Setup.php';
Setup::registerAutoloadDirectory($lib);
$paths = array("/path/to/entities-or-mapping-files");
$isDevMode = false;
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$dbParams = array(
'dbname' => 'mydb',
'user' => 'root',
'password' => '',
'host' => '127.0.0.1',
'driver' => 'pdo_mysql'
);
$em = EntityManager::create($dbParams, $config);
$helperSet = new HelperSet(array(
'db' => new ConnectionHelper($em->getConnection()),
'em' => new EntityManagerHelper($em)
));
ConsoleRunner::run($helperSet);
The error here.
Fatal error: Class 'Doctrine\DBAL\Tools\Console\Helper\EntityManagerHelper' not found in E:\wamp\www\project\doctrine.php on line 30
and I can not find EntityManagerHelper.php under DoctrineORM-2.2.0\Doctrine\DBAL\Tools\Console\Helper .
Seems like EntityManagerHelper is under different namespace:
namespace Doctrine\ORM\Tools\Console\Helper;

Categories