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

Advertisements

About Michal Zuber

Full stack developer, biker and rollerblader. Owner and developer at https://nevilleweb.sk/ Co-founded http://neville.sk/ Blog at https://michalzuber.wordpress.com/
This entry was posted in php and tagged , , , , . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s