After the dynamic meta data change (with code) Entity SQLs not building again and using old meta data 's SQLs for operations. #6611

Closed
opened 2026-01-22 15:35:42 +01:00 by admin · 1 comment
Owner

Originally created by @mdogancay on GitHub (Jan 20, 2021).

Hi,
After the $metadata->setPrimaryTable() or $metadata->setTableName() function calls; doctrine not doing reparse the Entity meta data and rebuild the SQLs for this entity. for example:

$metadata = $this->em->getClassMetadata(Product::class);
$metadata->setPrimaryTable([
    'name' => 'first_cat_product'
]);
echo $metadata ->getTableName() . "\n";
// got first_cat_product
$p = new Product();
$p
    ->setNameTr('test1')
    ->setCompanyId(1)
    ->setDescription('test1')
;
$this->em->persist($p);
$this->em->flush();
$this->em->clear();
unset($p, $metadata);

$metadata1 = $this->em->getClassMetadata(Product::class);
$metadata1->setPrimaryTable([
    'name' => 'second_cat_product'
]);
echo $metadata1->getTableName() . "\n";
// got second_cat_product
$p1 = new Product();
$p1
    ->setNameTr('test22')
    ->setCompanyId(1)
    ->setDescription('test22')
;
$this->em->persist($p1);
$this->em->flush();
$this->em->clear();
unset($p1, $metadata1);

That code always add both records to 'first_cat_product' table. Second meta data change ignoring.
Doctrine MUST reparse or rebuild the related SQLs after the meta data change for an Entity.
Otherwise meta data modification functions have no meaning.

Originally created by @mdogancay on GitHub (Jan 20, 2021). Hi, After the `$metadata->setPrimaryTable()` or `$metadata->setTableName()` function calls; doctrine not doing reparse the Entity meta data and rebuild the SQLs for this entity. for example: ``` $metadata = $this->em->getClassMetadata(Product::class); $metadata->setPrimaryTable([ 'name' => 'first_cat_product' ]); echo $metadata ->getTableName() . "\n"; // got first_cat_product $p = new Product(); $p ->setNameTr('test1') ->setCompanyId(1) ->setDescription('test1') ; $this->em->persist($p); $this->em->flush(); $this->em->clear(); unset($p, $metadata); $metadata1 = $this->em->getClassMetadata(Product::class); $metadata1->setPrimaryTable([ 'name' => 'second_cat_product' ]); echo $metadata1->getTableName() . "\n"; // got second_cat_product $p1 = new Product(); $p1 ->setNameTr('test22') ->setCompanyId(1) ->setDescription('test22') ; $this->em->persist($p1); $this->em->flush(); $this->em->clear(); unset($p1, $metadata1); ``` That code always add both records to 'first_cat_product' table. Second meta data change ignoring. Doctrine MUST reparse or rebuild the related SQLs after the meta data change for an Entity. Otherwise meta data modification functions have no meaning.
admin closed this issue 2026-01-22 15:35:42 +01:00
Author
Owner

@beberlei commented on GitHub (Feb 7, 2021):

Its not allowed to change metadata at runtime, only in MappingDriver and loadMetadata event

@beberlei commented on GitHub (Feb 7, 2021): Its not allowed to change metadata at runtime, only in MappingDriver and loadMetadata event
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6611