I am using the OnClassMetaDataNotFound event to implement dynamic table 
names. The idea is to use partitioning where different Entities share the 
same mapping, but differ only in tablename and entityname. I have mappings 
for a base class My\Base\Entity. No associations reference the dynamic 
(partitioned) entities and there is no inheritance.

I found the docs a bit poor on this point, but some reverse engineering 
suggests I need to change:

- table name
- entity name
- root entity name
- reflection class + reflection fields

The  problem is that the $name property is marked read-only in comments, 
which makes sense. Can it be changed for the purpose of implementing 
dynamic mappings like this or will it break Doctrine?

The event is handled more or less like this:

    
/**

 * @param OnClassMetadataNotFoundEventArgs $eventArgs
 */
public function onClassMetadataNotFound(OnClassMetadataNotFoundEventArgs 
$eventArgs)
{
    $className = $eventArgs->getClassName();

    if (strpos($className, 'My\Base\Entity') !== 0) {
        return;
    }

    $split = explode('_', $className);
    if (count($split) !== 2) {
        return;
    }

    $postfix = $split[1];

    /**
     * Reverse engineering suggest that this is what needs to be done
     * - load Metadata for My\Base\Entity
     * - modify table, name and rootEntityName
     * - set $eventArgs->foundMetaData
     */
    $baseMetaData = 
$eventArgs->getObjectManager()->getClassMetadata('My\Base\Entity');
    $metadata = clone($baseMetaData);

    $table = $metadata->table;
    $table['name'] = sprintf('%s_%s', $table['name'], $postfix);
    $metadata->setPrimaryTable($table);

    $metadata->name = $className;
    $metadata->rootEntityName = $className;


    $metadata->wakeupReflection(new RuntimeReflectionService());


$eventArgs->setFoundMetadata($metadata);
}

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to