Putting Doctrine entities in a non-standard directory

In a recent Symfony 2.3 project, I was trying out a new approach to code structure. In this brave new world, my models weren’t in the usual src/ProjectName/MyBundle/Entity directory. Rather, they were in src/MyProject/Models and src/MyProject/Admin/Models.

In this blog post, I’m not going to discuss the merits of this particular structure.  Instead, I wanted to outline how to tell Symfony and Doctrine where your models are to be found if they’re not in the default locations. I found this information rather hard to track down, so I’m hoping that this blog post will help others in my situation!

When I moved my models and didn’t change any configuration (which was what I tried first), I started seeing errors like this:

[Doctrine\Common\Persistence\Mapping\MappingException] 
The class 'MyProject\Models\User' was not found in the chain configured namespaces FOS\UserBundle\Entity

It was mentioning FOSUserBundle as my User entity inherited from the FriendsOfSymfony UserBundle’s User entity. Specifics aside, the problem is this – Doctrine doesn’t know where your entities are.

To fix this, you need to change the ORM (Object-Relational Mapping, i.e. Doctrine) settings in config.yml. In the Symfony Standard Edition, this section is very brief:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    auto_mapping: true

However, if you dig into the documentation for Symfony’s Doctrine configuration, you’ll see that you can set various options, including a path to entities!

My updated config looked like the below. You’ll see that there’s no auto_mapping line at all; it’s all replaced with explicit configuration. That’s why most of the settings for FOSUserBundle are null (~); I hadn’t moved the FOSUserBundle, so didn’t want to change the default settings.

orm:
  auto_generate_proxy_classes: %kernel.debug%
  mappings:
    MyProjectCoreBundle:
      mapping: true
      type: annotation
      dir: '../Models'
      alias: 'MyProjectCoreBundle'
      prefix: 'MyProject\Core\Models'
      is_bundle: true
    MyProjectAdminBundle:
      mapping: true
      type: annotation
      dir: '../Models'
      alias: 'MyProjectAdminBundle'
      prefix: 'MyProject\Admin\Models'
      is_bundle: true
    FOSUserBundle:
      mapping: true
      type: ~
      dir: ~
      alias: ~
      prefix: ~
      is_bundle: ~

Your project may require different settings depending on where you’ve put your entities, but the basic principle is the same. For more on these options, see the information about Mapping Configuration on Symfony’s documentation page about Doctrine configuration.

I won’t go into more detail here – the point of this blog post is simply to make clear that these options exist and that they can be used if you’re seeing the confusing “not found in the chain configured namespaces” error. Hope it helps, and happy code restructuring!

Post a Comment

Your email address is never published nor shared. Required fields are marked *

Ready to talk?

Whether you want to create a new digital product or make an existing one even better, we'd love to talk it through.

Get in touch