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:

// 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);
$entityManager = EntityManager::create($dbParams, $config);

$domain = $entityManager->getRepository('SF_PROJECT\CoreBundle\Entity\Domain')->find(1);
if ($domain === false) {
// 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*


Author: Michal Zuber

Full stack developer, biker and rollerblader. Owner and developer at Co-founded Blog at

One thought on “Using Symfony Entity via Doctrine in custom project”

  1. Thanks for the post, I was going through the same issue while deploying a symfony 4 app
    I had to configure the doctrine yaml file to set the autogenerate proxy classes as always

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s