commercetools / commercetools-php-sdk

The e-commerce SDK from commercetools for PHP.
https://docs.commercetools.com/sdk/php-sdk#php-sdk-v1
MIT License
43 stars 22 forks source link

way to create / import data using existing JSON as payload #147

Open nkuehn opened 9 years ago

nkuehn commented 9 years ago

please provide a (or document an existing) way to create / import data using JSON files.

example where it came up: a 3rd party tool generates product drafts as JSON which should be used in ProductCreateRequests instead of building the Product Drafts on the PHP API.

what happened is that the user did not use the PHP SDK for that but did it all manually (and ran into OAuth issues). It's propbably just documentation, but I think it would be helpful to provide explicit named constructors "ofDraftJson" or similar to either the Create Requests or the Draft Model Objects.

It's performance sensitive (batch imports), so there should not be a full object mapping roundtrip involved (validation is done platform-side anyways). JSON could be provided either as a String or as a PHP associative array.

eventhorizonpl commented 8 years ago

+1

Another use case would be replication of data from one database to another. ofObject would be a killer feature here.

barrycarton commented 8 years ago

+1

nkuehn commented 8 years ago

Hi, as there seems to be demand: Every Model Object in the SDK (in this case you're probably looking for the *Draft ones) can be created via its fromArray() function that takes the associative array created by PHP's own json_decode().
The same applies in the opposite direction: all the model objects are done to be cleanly serialized to JSON via json_encode($myCommercetoolsModel) .

So practically spoken the following (UNTESTED!) code is the pattern how you can do a bulk import using PHP:

<?php

use Commercetools\Core\Model\Product\ProductDraft;
use Commercetools\Core\Request\Products\ProductCreateRequest;
use Commercetools\Core\Client;
use Commercetools\Core\Config;

$client = Client::ofConfig(Config::fromArray(["clientId" => ""/* to do complete config*/]));

// for every batch of e.g. 100 products do:
    // for each product:
    $productDraftAsArray = json_decode(file_get_contents("./myFile.json"));
    $productDraft = ProductDraft::fromArray($productDraft);
    $addProductRequest = ProductCreateRequest::ofDraft($productDraft);
    $client->addBatchRequest($addProductRequest);
    //
$client->executeBatch();  // SDK will do the requests with a parallelity of (sensible default) 25
echo("added a batch of products");
//
// do next batch

That being said, IF you're importing Products, Prices (JSON or CSV) or other stuff (just CSV) you will still be faster and more robust using the Command-Line import / export tooling. Especially if you're updating / synchronizing and not just initially bulk importing. Generating the necessary update actions by "diffing" the current and new JSON is a real pain these tools do for you.
https://github.com/sphereio/sphere-product-import // http://sphereio.github.io/nodejs/

(@jayS-de FYI in case you have something better or tested available)