parse-community / parse-php-sdk

The PHP SDK for Parse Platform
https://parseplatform.org/
Other
811 stars 346 forks source link

It is possible to duplicate a parse object? #382

Closed ybroche closed 6 years ago

montymxb commented 6 years ago

@ybroche can you please provide more details? Are you talking about duplicating an instance of a ParseObject, creating a duplicate entry in parse server or something else?

ybroche commented 6 years ago

@montymxb I talk about clone or duplicate a document in mongodb, as if it were a new one, using parse-php-sdk.

montymxb commented 6 years ago

You would need to save a new parse object with the same data as the old one. If you've already saved the object you will need to take care to remove the objectId, otherwise you're just saving the same object again. Using clone will still retain the objectId, as would using serialize followed unserialize.

Depending on which sdk version you're using you can use ParseObject::encode and ParseObject::decode to serialize and unserialize an object in a way that you can manipulate it's data. While it's serialized you can decode and remove the objectId, effectively allowing you to decode an object with identical data that hasn't been saved yet.

$obj  = new ParseObject('MyClass');
$encoded = $obj->encode();
$decoded = json_decode($encoded, true);

// strip objectId out from the encoded object
unset($decoded['objectId');

// decode to an instance of ParseObject
$cloneObj = ParseObject::decode($decoded); // can also just pass $encoded, either works as it will be checked and decoded if needed

Something like the above is just one way to do this. Let me know if that's what you're looking for.

ybroche commented 6 years ago

@montymxb thank you very much for you answer, but I have a problem, this is my code based on your code. $query = new ParseQuery('MyClass'); $obj = $query->get($id); $encoded = $obj->encode(); $decoded = json_decode($encoded, true); unset($decoded['objectId']); $cloneObj = ParseObject::decode($decoded); $cloneObj->save();

When I save the cloned object, it only saves the objectid, createdAt, updateAt, ACL. Does not save the other fields of the original object

montymxb commented 6 years ago

@ybroche the reason for this is that those additional fields were probably already saved on your original object. The copy object will be in the same state as the original was, and saving only commits the changes you have made to it since it was last created/queried. I probably should have clarified that because of this you won't actually be saving the fields on an object if they have already been saved. Probably should have clarified that from the get go, sorry!

If you need to pull those fields off and set them on another object, you could retrieve all the keys and just set those values into a separate object entirely. Just to I would consider this better than the decoding/encoding approach I originally suggested, whether or not you're trying to copy data from a saved or unsaved object.

// $obj, your previously saved parse object
$copy = new ParseObject('MyClass');

// get all key/value pairs for your original object
$data = $copy->getAllKeys();

foreach($data as $key => $value) {
    $copy->set($key, $value);
}
// now you have a copy!
ybroche commented 6 years ago

@montymxb thank you very much for you answer. I had already analyzed the way you propose, but it is more complicated because I have to ask if the field is an array or an associative array to use setArray or setAssociativeArray, so I looked for alternatives.

montymxb commented 6 years ago

This does bring up a point that there is no straightforward way to do this. I'll put it on the TODO for the sdk. In the meantime if you're still stuck let us know, otherwise feel free to close this out.