DDC-4020: Dynamic entity mapping #4905

Closed
opened 2026-01-22 14:51:48 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 25, 2015).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user waldemarnt:

I need a way to create relations on the fly, and I choose the Event of the doctrine that is launched when load the class meta data, loadClassMetadata()

  public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
    $metadata = $eventArgs->getClassMetadata();

    $this->em = $eventArgs->getEntityManager();
    if ($metadata->getName() != 'AppBundle\Entity\NewsNews') {
        return;
    }
    $attachmentsMetadata = $this->getAttachmentsClassMetadata();

    $attachmentsMetadata->mapManyToOne(
        [
            "targetEntity" => $metadata->getName(),
            "fieldName"    => "newsNews",
            'joinColumns' => array(
                array(
                    'name' => 'foreign_key',
                    'referencedColumnName' => 'id'
                )
            ),
            "inversedBy" => "attachments"
        ]
    );
    $attachmentsMetadata->initializeReflection();

    $metadata->mapOneToMany(
        [
            "targetEntity" => $attachmentsMetadata->getName(),
            "fieldName"    => "attachments",
            'joinColumns' => array(
                array(
                    'name' => 'id',
                    'referencedColumnName' => 'foreign_key'
                )
            ),
            "mappedBy" => "newsNews"
        ]
    );
}

Ok, worked but the problem is when the doctrine will set the data for this relations he throw this exception "Notice: Undefined index: newsNews" I've checked the class when the doctrine will attach this data and the newsNews field is missing in the reflection properties. I don't know if I forget some part of this process xD

Thanks for the help

Originally created by @doctrinebot on GitHub (Nov 25, 2015). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user waldemarnt: I need a way to create relations on the fly, and I choose the Event of the doctrine that is launched when load the class meta data, loadClassMetadata() ``` php public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) { $metadata = $eventArgs->getClassMetadata(); $this->em = $eventArgs->getEntityManager(); if ($metadata->getName() != 'AppBundle\Entity\NewsNews') { return; } $attachmentsMetadata = $this->getAttachmentsClassMetadata(); $attachmentsMetadata->mapManyToOne( [ "targetEntity" => $metadata->getName(), "fieldName" => "newsNews", 'joinColumns' => array( array( 'name' => 'foreign_key', 'referencedColumnName' => 'id' ) ), "inversedBy" => "attachments" ] ); $attachmentsMetadata->initializeReflection(); $metadata->mapOneToMany( [ "targetEntity" => $attachmentsMetadata->getName(), "fieldName" => "attachments", 'joinColumns' => array( array( 'name' => 'id', 'referencedColumnName' => 'foreign_key' ) ), "mappedBy" => "newsNews" ] ); } ``` Ok, worked but the problem is when the doctrine will set the data for this relations he throw this exception "Notice: Undefined index: newsNews" I've checked the class when the doctrine will attach this data and the newsNews field is missing in the reflection properties. I don't know if I forget some part of this process xD Thanks for the help
admin added the Won't Fix label 2026-01-22 14:51:48 +01:00
admin closed this issue 2026-01-22 14:51:48 +01:00
Author
Owner

@Ang3 commented on GitHub (Apr 15, 2017):

Same problem with this simple code :

// Application de la ManyToOne
$metadata->mapManyToOne(array(
	'targetEntity' => $eventEntityAnnotation->targetEntity,
	'fieldName' => 'entity',
	'joinColumns' => array(array('entity' => 'entity_id')),
	'inversedBy' => 'events'
));

// Récupération des métadonnées de la classe cible des évènements
$targetMetadata = $entityManager->getClassMetadata($eventEntityAnnotation->targetEntity);

// Application de la OneToMany
$targetMetadata->mapOneToMany(array(
	'targetEntity' => $metadata->getName(),
	'cascade' => array('persist', 'remove'),
	'fieldName' => 'events', // ERROR ON THIS FIELD
	'mappedBy' => 'entity',
	'orderBy' => array('date' => 'ASC')
));
@Ang3 commented on GitHub (Apr 15, 2017): Same problem with this simple code : ```php // Application de la ManyToOne $metadata->mapManyToOne(array( 'targetEntity' => $eventEntityAnnotation->targetEntity, 'fieldName' => 'entity', 'joinColumns' => array(array('entity' => 'entity_id')), 'inversedBy' => 'events' )); // Récupération des métadonnées de la classe cible des évènements $targetMetadata = $entityManager->getClassMetadata($eventEntityAnnotation->targetEntity); // Application de la OneToMany $targetMetadata->mapOneToMany(array( 'targetEntity' => $metadata->getName(), 'cascade' => array('persist', 'remove'), 'fieldName' => 'events', // ERROR ON THIS FIELD 'mappedBy' => 'entity', 'orderBy' => array('date' => 'ASC') )); ```
Author
Owner

@Ocramius commented on GitHub (Apr 15, 2017):

The best you can do for now is emulating the behavior of the existing mapping drivers.

Closing here as won't fix, as @guilhermeblanco is creating a metadata builder for V3, which will likely change the API for this stuff entirely.

@Ocramius commented on GitHub (Apr 15, 2017): The best you can do for now is emulating the behavior of the existing mapping drivers. Closing here as `won't fix`, as @guilhermeblanco is creating a metadata builder for V3, which will likely change the API for this stuff entirely.
Author
Owner

@guilhermeblanco commented on GitHub (Apr 15, 2017):

This is already fixed in develop.

@guilhermeblanco commented on GitHub (Apr 15, 2017): This is already fixed in `develop`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4905