How do I use Doctrine’s Entity Generator?

Asked

Viewed 371 times

1

I’m trying to use Doctrine on a project. however I am having difficulty in being able to use Entity Generator once the database is ready and would like Doctrine to create the entities from it.

bootstrap.php

<?php
// o Autoload é responsável por carregar as classes sem necessidade de incluí-las previamente
require_once "vendor/autoload.php";

// o Doctrine utiliza namespaces em sua estrutura, por isto estes uses
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping;

//onde irão ficar as entidades do projeto? Defina o caminho aqui
$entidades = array("entity/");
$isDevMode = true;

// configurações de conexão. Coloque aqui os seus dados
$dbParams = array(
'driver'   => 'pdo_mysql',
'host'     => 'localhost',
'user'     => 'root',
'password' => 'senhadodb',
'dbname'   => 'LojaVirtual',
);

//setando as configurações definidas anteriormente
$config = Setup::createAnnotationMetadataConfiguration($entidades, $isDevMode);

//criando o Entity Manager com base nas configurações de dev e banco de dados
$entityManager = EntityManager::create($dbParams, $config);

I’m trying to create entities with the following code:

$cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
$cmf->setEntityManager($entityManager); // $em is EntityManager instance
$metadata = $cmf->getAllMetadata();
$generator = new \Doctrine\ORM\Tools\EntityGenerator();
$generator->setGenerateAnnotations(true);
$generator->setGenerateStubMethods(true);
$generator->setRegenerateEntityIfExists(true);
$generator->setUpdateEntityIfExists(false);
$generator->generate($metadata, 'entity');

I’ve tried that one too:

$classes = $entityManager->getMetadataFactory()->getAllMetadata();
$generator = new EntityGenerator();
$generator->setGenerateAnnotations(true);
$generator->setGenerateStubMethods(true);
$generator->setRegenerateEntityIfExists(false);
$generator->setUpdateEntityIfExists(true);
$generator->generate($classes, 'entity/');

I’ve never worked with Doctrine before, I’ve looked at documentation and various websites and nothing to find a way to make it work. I found some tutorials explaining to do by Command Line but they were not useful to me either.

I hope someone can tell me what is wrong and/ or what I should do to generate the entities from a db ready.

  • What problem are you having?

  • It always gives an error related to meta data, saying it could not load the valid metadata or something like that

  • You can glue the mistake so I get a better sense? :)

  • yes, I’m just waiting for Composer to install everything again for me to play the error again

  • @Rodrigorigotti could not reproduce the error, now the page only goes blank without returning any error.

  • @Rodrigorigotti have any tutorial suggestions for me to follow and do Entity Generator? I think I’ll close that question, I’ve done almost all the mapping manually anyway

  • Something pops up in the bug log? Tried to put the procedure inside a Try/catch block and see if there’s any Exception?

  • No need to close, let’s leave the question open so that other people who have the same mistake can solve.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.