Silex and Doctrine ORM
doctrine-orm, silex
Solution
I presume your doctrine Entity mappings reside under "/src/Entities" in the namespace `\Entities`. With your autoloader directive they should be accessible as `\Entities\MyMappingCls`.
Your problem seems to be that you don't give the fq-name of the mapping class when getting the repository. You need to give a string that can be resolved by the autoloader. Please try:
$app['orm.em']->getRepository('Entities\Customer');
You can also try to run `orm:generate-proxies` as they are only generated on the fly in debug mode (not so sure this is relevant).
hth
Problem
I am trying to use Silex together with Doctrine ORM (not just DBAL) but I am unable to get the configuration correct. composer.json ``` { "require": { "silex/silex": "1.0.*@dev", "symfony/monolog-bridge": "~2.1", "symfony/twig-bridge": "~2.1", "symfony/form": "~2.1", "symfony/yaml": "2.2.*", "symfony/form": "2.2.*", "symfony/translation": "~2.1", "symfony/config": "2.2.*", "dflydev/doctrine-orm-service-provider": "1.0.*@dev" }, "autoload": { "psr-0": { "Entities": "src/" } } } ``` bootstrap.php located in my project root folder ``` use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; require_once __DIR__ ."/vendor/autoload.php"; $isDevMode = true; $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/Entities"), $isDevMode); $params = array( 'driver' => 'pdo_sqlite', 'path' => __DIR__ . '/development.sqlite', ); $entityManager = EntityManager::create($params, $config); ``` cli-config.php also located inside the root folder ``` require_once "bootstrap.php"; $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array( 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($entityManager->getConnection()), 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($entityManager) )); ``` Customer.php entity located inside src/Entities ``` /** * @Entity @Table(name="customers") **/ class Customer { /** @Id @Column(type="integer") @GeneratedValue **/ protected $id; /** @Column(type="string") **/ protected $name; public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getId() { return $this->id; } } ``` I am able to run commands like `php vendor/bin/doctrine orm:schema-tool:create` and have it generate a table called customs just as it should. But how do I load that entity inside my Silex application Here is my index.php ``` require_once __DIR__.'/../vendor/autoload.php'; $app = new Silex\Application(); use Symfony\Component\Yaml\Yaml; $app['config'] = function () { $config = Yaml::parse(__DIR__ .'/../config.yml'); return $config; }; $app->register(new Silex\Provider\DoctrineServiceProvider(), array( 'dns.options' => $app['config']['database']['development'] )); $app->register(new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider, array( 'orm.em.options' => array( 'mappings' => array( array( 'type' => 'annotation', 'path' => __DIR__ .'/src/Entities', ) ) ), )); $app->get('/', function () use ($app) { $customer = $app['orm.em']->getRepository('Customer'); return '<pre>'. $customer->getName() .'</pre>'; }); ``` The result when loading the localhost inside my browser ``` Warning: class_parents() [function.class-parents]: Class Customer does not exist and could not be loaded in /Users/me/Documents/project/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php on line 40 ``` UPDATE I am not sure this is the correct way to solve this issue, but by using the following approach the problem got solved and I can now use my entities in Silex ``` $app['em'] = function ($app) { $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/Entities"), true); $params = array( 'driver' => 'pdo_sqlite', 'path' => __DIR__ . '/../development.sqlite', ); $entityManager = EntityManager::create($params, $config); return $entityManager; }; ``` I used the dependency approach because that way I can use $app['config'] to store DB information and other environment specific configurations. ``` $customer = new \Entities\Customer(); $customer->setName('Multi Corp '. uniqid()); $app['em']->persist($customer); $app['em']->flush(); ```