Propel2 icon indicating copy to clipboard operation
Propel2 copied to clipboard

Deep Copy: one-to-one models do not update PKs

Open McManning opened this issue 5 years ago • 0 comments

Hello,

We've ran into an issue with the behavior of deep copy on models when migrating over from Propel 1 to 2 on some of our older applications - which looks like it could be a bug in the generated code.

Given the following schema:

<table name="Parent">
    <column name="parentId" type="integer"
        required="true" primaryKey="true" autoIncrement="true" />

    <!-- other columns omitted... -->
</table>

<table name="Child">
    <column name="parentId" type="integer"
        required="true" primaryKey="true" />

    <!-- other columns omitted... -->

    <foreign-key foreignTable="Parent" onDelete="CASCADE">
        <reference local="parentId" foreign="parentId"/>
    </foreign-key>
</table>

And the call:

$parent = ParentQuery::create()->yadda yadda
$copy = $parent->copy($deepCopy=true);

I would expect that the Child model be associated with the new parent, but that's not the case. The Child is copied to a new record, but the primary key still references the prior Parent model.

Digging through the generated code a bit, I'm seeing that in Parent::copyInto it does the copy and assignment correctly:

$relObj = $this->getChild();
if ($relObj) {
    $copyObj->setChild($relObj->copy($deepCopy));
}

But - in the Parent::setChild method the below condition will never hold true since the child already has a Parent associated (the copied-from) so Child::setParent is never called to create a back reference:

if ($v !== null && $v->getParent(null, false) === null) {
    $v->setParent($this);
}

In ObjectBuilder.php, I'm seeing the following note that might explain why this behavior changed - but unfortunately the link is dead:

// Note: we're no longer resetting non-autoincrement primary keys to default values
// due to: http://propel.phpdb.org/trac/ticket/618

This wasn't a problem in Propel 1 - and a feature we use pretty heavily for large (~30+ model) relationships that are copied around as part of a versioning framework.

So I'm hoping someone remembers why this behavior may have changed and maybe have some guidance on how this could be resolved?

Thanks! Chase

McManning avatar Apr 03 '19 19:04 McManning