20

The configuration uses :

doctrine:
dbal:
  driver:   "%database_driver%"
   ....
orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    auto_mapping: true

What is the exact meaning of auto_mapping? It is used in tons of examples with true and false, and no precise description. When does occurs the proxy generation if it's not auto ? By doctrine command-line tools ?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nicolas Zozol
  • 6,910
  • 3
  • 50
  • 74

2 Answers2

16

auto_mapping is where doctrine will automatically load the mapping from your bundle Resources/config/doctrine directory.

Setting it to false will mean that you will need to load the mappings yourself. It can be handy if you have mappings for entities rather than mapped superclasses in a vendor bundle that you want to override.

You can do this either by way of stating the mappings in the doctrine config ...

doctrine:
    orm:
        entity_managers:
            default:
                mappings:
                    AcmeUnknownBundle:
                        mapping:              true
                        type:                 yml
                        dir:                  "Resources/config/doctrine"
                        alias:                ~
                        prefix:               Acme\UnknownBundle\Entity
                        is_bundle:            true

adding them in some sort of mappings pass ...

class AcmeUnknownBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        // ...

        $modelDir = realpath(__DIR__.'/Resources/config/doctrine/model');
        $mappings = array(
            $modelDir => 'Acme\UnknownBundle\Model',
        );

        $ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
        if (class_exists($ormCompilerClass)) {
            $container->addCompilerPass(
                DoctrineOrmMappingsPass::createYamlMappingDriver(
                    $mappings,
                    array('acme_unknown.model_manager_name'),
                    true
            ));
        }
    }
}
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
qooplmao
  • 17,622
  • 2
  • 44
  • 69
2

I know this is super late, but the answer to the second part of the question regarding auto_generate_proxy_classes can be found in Doctrine's ORM documentation, here: Auto-Generating Proxy Classes (ORM v2.11 docs).

I recommend also reading the "Proxy Objects" section a little further down on the same page. That section says, in part:

A proxy object is an object that is put in place or used instead of the real object. A proxy object can add behavior to the object being proxied without that object being aware of it. In ORM, proxy objects are used to realize several features but mainly for transparent lazy-loading.

Proxy objects with their lazy-loading facilities help to keep the subset of objects that are already in memory connected to the rest of the objects. This is an essential property as without it there would always be fragile partial objects at the outer edges of your object graph.

Doctrine ORM implements a variant of the proxy pattern where it generates classes that extend your entity classes and adds lazy-loading capabilities to them. Doctrine can then give you an instance of such a proxy class whenever you request an object of the class being proxied.

WARNING: The ORM docs are very clear that this should be allowed in development only. Read more at the link above, but the bottom line is this:

Furthermore you should have the Auto-generating Proxy Classes option to true in development and to false in production. If this option is set to TRUE it can seriously hurt your script performance if several proxy classes are re-generated during script execution. Filesystem calls of that magnitude can even slower than all the database queries Doctrine issues. Additionally writing a proxy sets an exclusive file lock which can cause serious performance bottlenecks in systems with regular concurrent requests.

tl;dr -- and I know that I'm glossing over a lot of nuances -- this is somewhat like a PHP version of Javascript's Promise model. Don't have an actual object yet? No worries, we'll give you a fake (proxy) one and fill in the details later if you need them. The auto_generate_proxy_classes setting defines whether or not this is done on the fly at runtime.

For those who want more detailed info on the Proxy Pattern in PHP, try here or here.

jetsetter
  • 517
  • 5
  • 19