symfony - Doctrine2: Unable to override generated value strategy? -
i have entity id such:
/** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id;
i'm migrating data entity, , want preserve existing keys. looked @ "explicitly set id doctrine when using "auto" strategy" , found should able following:
$newdata = ... // array containing data bring in $newentity = new myentity(); $newentity->setid($newdata['id']); $newentity->... // set other data fields $em->persist($newentity); $metadata = $em->getclassmetadata('\cs\acmebundle\entity\myentity'); $metadata->setidgenerator(new \doctrine\orm\id\assignedgenerator()); $em->flush();
however, doctrine not using provided id. it's ignoring when inserting. i've tried approach instead, since people seemed have had luck (even tried both):
$metadata->setidgeneratortype(\doctrine\orm\mapping\classmetadata::generator_type_none);
but doesn't change anything. id's still inserted automatically database. in query log, see doctrine isn't attempting insert id.
if remove @orm\generatedvalue(strategy="auto")
myentity annotations, migration respect provided id give it. want override during migration.
i'm using doctrine 2.4.2.
for technique work, must use second of these:
$metadata = $em->getclassmetadata('\cs\acmebundle\entity\myentity'); $metadata = $em->getclassmetadata('cs\acmebundle\entity\myentity');
the problem doctrine return same class meta data values both. both correctly identify class file, read annotations, etc. equivalent, except 1 absolute namespace , other not.
but these strings return different instances getclassmetadata
. changes 1 won't reflect in other. if want intended technique work, must use second form, because unitofwork
uses. uses normalization:
// \doctrine\orm\unitofwork->getcommitorder() ... $classname = $this->em->getclassmetadata(get_class($entity))->name; $class = $this->em->getclassmetadata($classname); ...
note in linked-to question, solution uses get_class($entity)
. sufficient correct behavior.
even more detail: after lot of stepping through code, noticed \doctrine\common\persistence\mapping\abstractclassmetadatafactory
memoizing both versions of class name string in private property $loadedmetadata
. version being used flush entities 1 without leading slash, , editing 1 leading slash.
because both strings return same data, think represents bug in implementation.
Comments
Post a Comment