Getting CREATE SQL for single Doctrine entity in Symfony2

I needed to create a new table for a single new Doctrine entity into an existing database.

I was in hurry and couldn’t find a solution so came up with my own.

Dump the whole SQL into a file:

php app/console doctrine:schema:create --dump-sql > dump.sql

For fixing lowercase table name I found a solution at http://kurtfunai.com/2012/09/doctrine-table-name.html

/**
 * NAMESPACE\ExampleBundle\Entity\User
 *
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User
{
Advertisements

Using Symfony Entity via Doctrine in custom project

With my colleague we had to code a custom code for a legacy/custom codebase that would use Symfony2 entities via Doctrine for a couple of months.

composer.json included the following:

{
    "minimum-stability": "stable",
    "require": {
        "php": ">=5.3.9",
        "doctrine/orm": "2.4.*",
        "symfony/symfony": "2.7.*",
        "symfony/dependency-injection": "2.7.*",
        "symfony/validator": "2.7.*",
        "symfony/doctrine-bridge": "2.7.*"
    },
    "autoload": {
        "psr-0": {
            "": "src/"
        }
    }
}

We we’re getting the following PHP error:

Warning: require(/var/tmp/__CG__CoreBundleEntityProduct.php): failed to open stream: No such file or directory in /Volumes/data/Sites/legacy-proj/trunk/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php on line 209

Fatal error: require(): Failed opening required '/var/tmp/__CG__CoreBundleEntityProduct.php' (include_path='.:') in /Volumes/data/Sites/legacy-proj/trunk/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php on line 209

The solution was to regenerate the proxy classes. The finale code looked like this:

<?php
// src/Wrapper/bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$loader = require_once __DIR__.'/autoload.php';

$paths = array(dirname(__FILE__).'/../../src/SF_PROJECT/CoreBundle/Entity');
$isDevMode = false;

$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'centralis',
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
$config->setAutoGenerateProxyClasses(true);
$entityManager = EntityManager::create($dbParams, $config);

$domain = $entityManager->getRepository('SF_PROJECT\CoreBundle\Entity\Domain')->find(1);
if ($domain === false) {
    return;
}
<?php
// src/Wrapper/autoload.php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

/**
 * @var ClassLoader $loader
 */
$loader = require __DIR__.'/../../vendor/autoload.php';

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;

After that the code generated the following classes in /var/tmp

mike@mike ~ $ ls /var/tmp/__CG*
/var/tmp/__CG__AtechNetCoreBundleEntityAlbum.php
/var/tmp/__CG__AtechNetCoreBundleEntityAttribute.php
/var/tmp/__CG__AtechNetCoreBundleEntityAttributeOption.php
/var/tmp/__CG__AtechNetCoreBundleEntityCustomer.php
/var/tmp/__CG__AtechNetCoreBundleEntityDomain.php
/var/tmp/__CG__AtechNetCoreBundleEntityLanguage.php
/var/tmp/__CG__AtechNetCoreBundleEntityStore.php
/var/tmp/__CG__AtechNetCoreBundleEntityStoreMovement.php
/var/tmp/__CG__AtechNetCoreBundleEntityUser.php
/var/tmp/__CG__AtechNetCoreBundleEntityVendor.php