parse-php-sdk icon indicating copy to clipboard operation
parse-php-sdk copied to clipboard

Can't perform batch operations after migration

Open lipusal opened this issue 10 years ago • 10 comments

I've recently completed migrating my Parse PHP application to Google Cloud-hosted Parse Server. Since then, batch operations like ParseObject::saveAll() and ParseObject::destroyAll() started to fail in most cases.

I can't even reproduce the SDK's destroyAll() test function.

In that example, when sending the request for saveAll() Parse Server returns error code 107, which according to docs means invalid JSON, but the sent request is valid JSON:

{"requests":[{"method":"POST","path":"/1/classes/TestObject","body":[]},{"method":"POST","path":"/1/classes/TestObject","body":[]},{"method":"POST","path":"/1/classes/TestObject","body":[]}]}

This causes a ParseException with the message:

Uncaught exception 'Parse\ParseException' with message 'cannot route batch path /1/classes/TestObject'

I'm running this with PHP 5.6. The current solution is to call save() or destroy() for each object individually, but this is extremely inefficient. Thanks for your attention.

lipusal avatar Mar 05 '16 01:03 lipusal

It seems strange your paths are including an api version. With the new ParseServer you should be finding your resources at /classes/TestObject instead of /1/classes/TestObject. I'd make sure you're calling 'setServerURL' right after parse initialize

ParseClient::initialize( $app_id, $rest_key, $master_key );
// Users of Parse Server will need to point ParseClient at their remote URL:
ParseClient::setServerURL('https://your-server.com/parse');

Other than that I did happen to notice what looks like a bug in the following code in ParseObject.php in private static function deepSave($target, $useMasterKey = false)

foreach ($requests as &$r) {
    $r['path'] = '/1/'.$r['path'];
}

That most likely is the issue in your case. I would try remove the '/1/' being added to the path and see if that works out. This is probably a bug that needs to be patched

montymxb avatar Mar 05 '16 04:03 montymxb

@montymxb I am calling setServerURL() with the new URL and the rest of the functions work properly, it's just batch operations that throw this error. I also posted this issue on StackOverflow and received the same suggestion, but that throws the same error (with the new route), whether I remove "/1/" or "/1"

lipusal avatar Mar 05 '16 12:03 lipusal

@lipusal I dug into this a bit more. It turns out the server does some stripping on the urls it receives for batch requests. Specifically it checks to see that your api prefix (/parse in most cases) matches what was sent. With batch requests the individual requests do not contain this, and they end up failing this match. Testing out the code you put up on StackOverflow I was able to patch it up. It works, but I'll probably see if I can't write up a better patch in the future.

You currently need to prepend your api prefix as /your_prefix/ (again in most cases this is /parse, but use the one you've set up for your server) in two places for batched saves and deletes as follows.

ParseObject.php private static function deepSave($target, $useMasterKey = false) (same place where /1/ was previously prepended)

foreach ($requests as &$r) {
    $r['path'] = '/parse/' . $r['path'];
}

ParseObject.php private static function destroyBatch(array $objects, $useMasterKey = false)

foreach ($objects as $object) {
    $data[] = [
        'method' => 'DELETE',
        'path'   => '/parse/classes/'.$object->getClassName().'/'.$object->getObjectId(),
    ];
}

With those two spots changed I was able to get batched saves & deletes running properly. I also observed the same issue you had before just to confirm. I've sent a pull request as of yesterday to address this, but I need to roll in a fix that includes the user's mount path in a less hacky way. In the meantime try this out and see if this works for you

montymxb avatar Mar 05 '16 20:03 montymxb

@montymxb Sorry about the slow response. I'm happy to say that your suggestion has the batch functions working again. I used /parse/ as prefix as per your code suggestions. Will look forward to an official fix later on. If any other issues come up I'll reference them here if appropriate.

Thanks!

lipusal avatar Mar 06 '16 20:03 lipusal

Glad to hear it :+1: . There's a pull request pending to patch this as well

montymxb avatar Mar 06 '16 21:03 montymxb

This saved me a huge headache. Thank you!

jjonesdesign avatar Jun 13 '16 23:06 jjonesdesign

Is this already fixed in latest parse-server?

Thanks and best, Chris

code-K avatar Jun 17 '16 15:06 code-K

@code-K Unfortunately I believe this is still an issue in the php sdk. You can reference the proposed fix mentioned earlier in this issue if you need to get something running. Alternatively pull request #228 contains this fix, if you want to try that out.

montymxb avatar Jun 17 '16 17:06 montymxb

Merged #228. Hope this helps, thanks.

gfosco avatar Jun 20 '16 13:06 gfosco

Do I need the latest Parse JavaScript SDK on client end for this to work? For some super cool reasons I cannot update client Parse SDK from 1.5.0 to 1.9.0, and I am having this issue. It works if I mount parse server to "/1".

cipiripper avatar Jul 22 '16 10:07 cipiripper