Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

duplicate a record

I would like to duplicate a record without having to assign each field.

Similarly to what happens in the default CRUD delete handler I tried:

$product = new products\Product ($_POST['id']);
$product->id = null;
$product->keyval = false;
$product->title .= __(" (duplicate)");
$product->put ();

but it doesn't work.

Based on what the method put() chooses the insert or the update?

Thanks.

Comments

  • I assure you that I have combed the Model class before asking.

    Only now I notice the is_new property.

    Please bear me if you can.

  • Looking at some "duplicate this" code I wrote last week, here's what I did:

    $product = new products\Product ($_POST['id']);
    
    // prepare copy and reset any properties
    $copy = $product->orig ();
    unset ($copy['id']);
    $copy['title'] .= __ (' (duplicate)');
    
    // create duplicate
    $new = new products\Product ($copy);
    $new->put ();
    

    Hope that helps.

  • Yes it helps, thanks.

    Before you answer I had managed in this way that seems to work but I'm not sure it's a good way:

    $product = new products\Product ($_POST['id']);
    $product->id = null;
    $product->is_new = true;
    $product->title .= __(" (duplicate)");
    $product->put ();
    

    What do you say?

  • That should work pretty much the same, although yours might actually be a bit faster :)

Sign In or Register to comment.